Module: Proscenium::SideLoad::Controller

Defined in:
lib/proscenium/side_load.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(child) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/proscenium/side_load.rb', line 9

def self.included(child)
  child.class_eval do
    class_attribute :sideload_assets_options
    child.extend ClassMethods

    append_after_action :capture_and_replace_proscenium_stylesheets,
                        :capture_and_replace_proscenium_javascripts,
                        if: -> { response.content_type&.include?('html') }
  end
end

Instance Method Details

#capture_and_replace_proscenium_javascriptsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/proscenium/side_load.rb', line 56

def capture_and_replace_proscenium_javascripts
  return if response_body.nil?
  return if response_body.first.blank? || !Proscenium::Importer.js_imported?

  included_comment = response_body.first.include?(JS_COMMENT)
  fragments = if (fragment_header = request.headers['X-Fragment'])
                fragment_header.split
              end

  return if !fragments && !included_comment

  out = []
  Proscenium::Importer.each_javascript(delete: true) do |path, opts|
    next if opts.delete(:lazy)

    opts = opts[:js].is_a?(Hash) ? opts[:js] : {}
    opts[:preload_links_header] = false if fragments

    if Proscenium.config.cache_query_string.present?
      path += "?#{Proscenium.config.cache_query_string}"
    end
    out << helpers.javascript_include_tag(path.delete_prefix('/'), extname: false, **opts)
  end

  if fragments
    response_body.first.prepend out.join.html_safe
  elsif included_comment
    response_body.first.gsub! JS_COMMENT, out.join.html_safe
  end
end

#capture_and_replace_proscenium_stylesheetsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/proscenium/side_load.rb', line 26

def capture_and_replace_proscenium_stylesheets
  return if response_body.nil?
  return if response_body.first.blank? || !Proscenium::Importer.css_imported?

  included_comment = response_body.first.include?(CSS_COMMENT)
  fragments = if (fragment_header = request.headers['X-Fragment'])
                fragment_header.split
              end

  return if !fragments && !included_comment

  out = []
  Proscenium::Importer.each_stylesheet(delete: true) do |path, opts|
    opts = opts[:css].is_a?(Hash) ? opts[:css] : {}
    opts[:preload_links_header] = false if fragments
    opts[:data] ||= {}

    if Proscenium.config.cache_query_string.present?
      path += "?#{Proscenium.config.cache_query_string}"
    end
    out << helpers.stylesheet_link_tag(path.delete_prefix('/'), extname: false, **opts)
  end

  if fragments
    response_body.first.prepend out.join.html_safe
  elsif included_comment
    response_body.first.gsub! CSS_COMMENT, out.join.html_safe
  end
end