Module: CriticalHelper

Defined in:
app/helpers/critical_helper.rb

Instance Method Summary collapse

Instance Method Details

#critical_css(params = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/critical_helper.rb', line 31

def critical_css(params = {})
  scope = params.fetch(:scope, 'critical')
  name = find_scoped_css(scope)
  stylesheets = Array.wrap(params.fetch(:stylesheets, []))
  data = StringIO.new

  if name.blank?
    # insert stylesheets directly if not crit css
    data << '<!-- CRIT CSS NOT FOUND! -->'
    data << stylesheet_link_tag(*stylesheets) if stylesheets.present?
  else
    data << inline_css(name)
    if stylesheets.present?
      preload_css = css_preload(stylesheets)
      if block_given?
        yield preload_css
      else
        data << preload_css
      end
    end
    data << inline_js('crit-utils/measure.js') if Rails.env.development?
  end

  data.string.html_safe
end

#critical_js(bundle: 'crit-utils/bundle.js') ⇒ Object



26
27
28
29
# File 'app/helpers/critical_helper.rb', line 26

def critical_js(bundle: 'crit-utils/bundle.js')
  inline_js(bundle).presence || inline_js('crit-utils/bundle.js').presence ||
    '<!-- CRIT JS NOT FOUND! -->'.html_safe
end

#css_preload(names) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/critical_helper.rb', line 4

def css_preload(names)
  return '' if names.blank?
  names = Array.wrap(names)

  link = stylesheet_link_tag(*names, rel: 'preload', as: 'style', onload: 'this.rel="stylesheet"')
  noscript =  :noscript do
    stylesheet_link_tag(*names)
  end

  link + noscript
end

#debug_critical_css(scope_name = 'critical') ⇒ Object



63
64
65
66
67
68
# File 'app/helpers/critical_helper.rb', line 63

def debug_critical_css(scope_name = 'critical')
  {
    lookup: names_for_critical_lookup(scope_name),
    found: find_scoped_css(scope_name)
  }.to_json.html_safe
end

#inline_css(name) ⇒ Object



20
21
22
23
24
# File 'app/helpers/critical_helper.rb', line 20

def inline_css(name)
  raw = IziLightup::InlineAsset.inline_css(name).html_safe
  raw = "<!-- CRIT CSS: #{name} -->".html_safe + raw if Rails.env.development?
  raw
end

#inline_js(name) ⇒ Object



16
17
18
# File 'app/helpers/critical_helper.rb', line 16

def inline_js(name)
  IziLightup::InlineAsset.inline_js(name).html_safe
end

#names_for_critical_lookup(scope_name = 'critical', extname = '.css') ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/critical_helper.rb', line 70

def names_for_critical_lookup(scope_name = 'critical', extname = '.css')
  names = [
    [controller_path, action_name].compact.join('_'),
    controller_path,
    *scoped_namespace_file,
    [controller_name, action_name].compact.join('_'),
    controller_name,
  ].reject(&:blank?).uniq
  names << nil

  names.map { |l| "#{File.join([scope_name, l].compact)}#{extname}" }
end

#smart_picture(object, *args, **xargs, &block) ⇒ Object



57
58
59
60
61
# File 'app/helpers/critical_helper.rb', line 57

def smart_picture(object, *args, **xargs, &block)
  return '' if object.blank?

  IziLightup::SmartPicture.render(object, *args, **xargs, &block)
end