Module: Proscenium::SideLoad::Controller
- Defined in:
- lib/proscenium/side_load.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #capture_and_replace_proscenium_javascripts ⇒ Object
- #capture_and_replace_proscenium_stylesheets ⇒ Object
Class Method Details
.included(child) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/proscenium/side_load.rb', line 10 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_javascripts ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/proscenium/side_load.rb', line 70 def capture_and_replace_proscenium_javascripts return if response_body.nil? return if response_body.first.blank? || !Proscenium::Importer.js_imported? imports = Proscenium::Importer.imported.dup paths_to_build = [] Proscenium::Importer.each_javascript(delete: true) do |x, _| paths_to_build << x.delete_prefix('/') end result = Proscenium::Builder.build_to_path(paths_to_build.join(';'), base_url: helpers.request.base_url) included_js_comment = response_body.first.include?(JS_COMMENT) included_lazy_comment = response_body.first.include?(LAZY_COMMENT) fragments = if (fragment_header = request.headers['X-Fragment']) fragment_header.split end if fragments || included_js_comment out = [] scripts = {} result.split(';').each do |x| inpath, outpath = x.split('::') inpath.prepend '/' outpath.delete_prefix! 'public' next unless imports.key?(inpath) if (import = imports[inpath]).delete(:lazy) scripts[inpath] = import.merge(outpath:) else opts = import[:js].is_a?(Hash) ? import[:js] : {} opts[:preload_links_header] = false if fragments out << helpers.javascript_include_tag(outpath, extname: false, **opts) end end if fragments response_body.first.prepend out.join.html_safe elsif included_js_comment response_body.first.gsub! JS_COMMENT, out.join.html_safe end end return if !fragments && !included_lazy_comment lazy_script = '' if scripts.present? lazy_script = helpers.content_tag 'script', type: 'application/json', id: 'prosceniumLazyScripts' do scripts.to_json.html_safe end end if fragments response_body.first.prepend lazy_script elsif included_lazy_comment response_body.first.gsub! LAZY_COMMENT, lazy_script end end |
#capture_and_replace_proscenium_stylesheets ⇒ Object
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/proscenium/side_load.rb', line 27 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 imports = Proscenium::Importer.imported.dup paths_to_build = [] Proscenium::Importer.each_stylesheet(delete: true) do |x, _| paths_to_build << x.delete_prefix('/') end result = Proscenium::Builder.build_to_path(paths_to_build.join(';'), base_url: helpers.request.base_url) out = [] result.split(';').each do |x| inpath, outpath = x.split('::') inpath.prepend '/' outpath.delete_prefix! 'public' next unless imports.key?(inpath) import = imports[inpath] opts = import[:css].is_a?(Hash) ? import[:css] : {} opts[:preload_links_header] = false if fragments opts[:data] ||= {} opts[:data][:original_href] = inpath out << helpers.stylesheet_link_tag(outpath, 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 |