Skip to content

Commit

Permalink
feat: Sideloaded assets are now prepended to the response body when t…
Browse files Browse the repository at this point in the history
…he Phlex fragment header is present
joelmoss committed Nov 15, 2024
1 parent 083db81 commit 282c1f6
Showing 2 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/proscenium/helper.rb
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ def include_assets
end

def include_stylesheets
'<!-- [PROSCENIUM_STYLESHEETS] -->'.html_safe
SideLoad::CSS_COMMENT.html_safe
end
alias side_load_stylesheets include_stylesheets
deprecate side_load_stylesheets: 'Use `include_stylesheets` instead', deprecator: Deprecator.new
@@ -68,7 +68,7 @@ def include_stylesheets
#
# @return [String] the HTML tags for the javascripts.
def include_javascripts
'<!-- [PROSCENIUM_LAZY_SCRIPTS] --><!-- [PROSCENIUM_JAVASCRIPTS] -->'.html_safe
(SideLoad::LAZY_COMMENT + SideLoad::JS_COMMENT).html_safe
end
alias side_load_javascripts include_javascripts
deprecate side_load_javascripts: 'Use `include_javascripts` instead', deprecator: Deprecator.new
42 changes: 36 additions & 6 deletions lib/proscenium/side_load.rb
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@

module Proscenium
class SideLoad
JS_COMMENT = '<!-- [PROSCENIUM_JAVASCRIPTS] -->'
CSS_COMMENT = '<!-- [PROSCENIUM_STYLESHEETS] -->'
LAZY_COMMENT = '<!-- [PROSCENIUM_LAZY_SCRIPTS] -->'

module Controller
def self.included(child)
child.class_eval do
@@ -23,7 +27,13 @@ def sideload_assets(value)
def capture_and_replace_proscenium_stylesheets
return if response_body.nil?
return if response_body.first.blank? || !Proscenium::Importer.css_imported?
return unless response_body.first.include? '<!-- [PROSCENIUM_STYLESHEETS] -->'

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 = []
@@ -44,12 +54,17 @@ def capture_and_replace_proscenium_stylesheets

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

response_body.first.gsub! '<!-- [PROSCENIUM_STYLESHEETS] -->', out.join.html_safe
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

def capture_and_replace_proscenium_javascripts
@@ -65,7 +80,13 @@ def capture_and_replace_proscenium_javascripts
result = Proscenium::Builder.build_to_path(paths_to_build.join(';'),
base_url: helpers.request.base_url)

if response_body.first.include? '<!-- [PROSCENIUM_JAVASCRIPTS] -->'
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|
@@ -79,14 +100,19 @@ def capture_and_replace_proscenium_javascripts
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

response_body.first.gsub! '<!-- [PROSCENIUM_JAVASCRIPTS] -->', out.join.html_safe
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 unless response_body.first.include? '<!-- [PROSCENIUM_LAZY_SCRIPTS] -->'
return if !fragments && !included_lazy_comment

lazy_script = ''
if scripts.present?
@@ -96,7 +122,11 @@ def capture_and_replace_proscenium_javascripts
end
end

response_body.first.gsub! '<!-- [PROSCENIUM_LAZY_SCRIPTS] -->', lazy_script
if fragments
response_body.first.prepend lazy_script
elsif included_lazy_comment
response_body.first.gsub! LAZY_COMMENT, lazy_script
end
end
end

0 comments on commit 282c1f6

Please sign in to comment.