DEV: Add link rel preload to theme-javascripts (#19231)

* DEV: Add link rel preload to theme-javascripts
This commit is contained in:
Isaac Janzen 2022-11-30 12:43:01 -06:00 committed by GitHub
parent 07fb7fc54d
commit aea492df5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -6,7 +6,7 @@ require 'json_schemer'
class Theme < ActiveRecord::Base
include GlobalPath
BASE_COMPILER_VERSION = 67
BASE_COMPILER_VERSION = 68
attr_accessor :child_components
@ -394,7 +394,12 @@ class Theme < ActiveRecord::Base
end
caches = JavascriptCache.where(theme_id: theme_ids)
caches = caches.sort_by { |cache| theme_ids.index(cache.theme_id) }
return caches.map { |c| "<script defer src='#{c.url}' data-theme-id='#{c.theme_id}'></script>" }.join("\n")
return caches.map do |c|
<<~HTML.html_safe
<link rel="preload" href="#{c.url}" as="script">
<script defer src='#{c.url}' data-theme-id='#{c.theme_id}'></script>
HTML
end.join("\n")
end
list_baked_fields(theme_ids, target, name).map { |f| f.value_baked || f.value }.join("\n")
end

View File

@ -148,7 +148,14 @@ class ThemeField < ActiveRecord::Base
javascript_cache.source_map = js_compiler.source_map
javascript_cache.save!
doc.add_child("<script defer src='#{javascript_cache.url}' data-theme-id='#{theme_id}'></script>") if javascript_cache.content.present?
if javascript_cache.content.present?
doc.add_child(
<<~HTML.html_safe
<link rel="preload" href="#{javascript_cache.url}" as="script">
<script defer src='#{javascript_cache.url}' data-theme-id='#{theme_id}'></script>
HTML
)
end
[doc.to_s, errors&.join("\n")]
end
@ -241,7 +248,13 @@ class ThemeField < ActiveRecord::Base
javascript_cache.source_map = js_compiler.source_map
javascript_cache.save!
doc = ""
doc = "<script defer src='#{javascript_cache.url}' data-theme-id='#{theme_id}'></script>" if javascript_cache.content.present?
if javascript_cache.content.present?
doc =
<<~HTML.html_safe
<link rel="preload" href="#{javascript_cache.url}" as="script">
<script defer src='#{javascript_cache.url}' data-theme-id='#{theme_id}'></script>
HTML
end
[doc, errors&.join("\n")]
end