Skip to content

A platform for community discussion. Free, open, simple.

PERF: Move theme settings from js bundle to preload data (#40137)

David Taylord42c648fc77f
diff --git a/app/helpers/qunit_helper.rb b/app/helpers/qunit_helper.rb
index c1025e06814..5171ed55712 100644
--- a/app/helpers/qunit_helper.rb
+++ b/app/helpers/qunit_helper.rb
@@ -13,4 +13,22 @@ module QunitHelper
"?__ws=#{Discourse.current_hostname}"
"<link rel='modulepreload' href='#{src}' data-theme-id='#{theme.id}' data-theme-name='#{Rack::Utils.escape_html(theme.name)}' nonce='#{ThemeField::CSP_NONCE_PLACEHOLDER}' />".html_safe
end
+
+ def fake_preload_data
+ preloaded = {}
+ preloaded.merge!(theme_settings_preload_data)
+ return "" if preloaded.empty?
+
+ tag.div("", id: "data-preloaded", data: { preloaded: preloaded.to_json })
+ end
+
+ private
+
+ def theme_settings_preload_data
+ theme = Theme.find_by(id: request.env[:resolved_theme_id])
+ return {} if theme.blank?
+
+ activated_themes = { theme.id => { name: theme.name, settings: theme.cached_default_settings } }
+ { "activatedThemes" => activated_themes.to_json }
+ end
end
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 1faebdcdf0f..e71ce54a2c9 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -381,9 +381,14 @@ class RemoteTheme < ActiveRecord::Base
end
transaction_block = ->(*) do
- # Destroy fields that no longer exist in the remote theme
field_ids_to_destroy = theme.theme_fields.pluck(:id) - updated_fields.map { |tf| tf&.id }
- ThemeField.where(id: field_ids_to_destroy).destroy_all
+ theme
+ .theme_fields
+ .where(id: field_ids_to_destroy)
+ .each do |field|
+ field.mark_for_destruction
+ theme.changed_fields << field
+ end
update_theme_color_schemes(theme, theme_info["color_schemes"]) unless theme.component
diff --git a/app/models/theme.rb b/app/models/theme.rb
index c3d560cd84c..c2eb18a9b26 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -151,12 +151,15 @@ class Theme < ActiveRecord::Base
any_non_css_fields_changed =
changed_fields.any? { |f| !(f.basic_scss_field? || f.extra_scss_field?) }
+ any_extra_js_fields_changed = changed_fields.any?(&:extra_js_field?)
- changed_fields.each(&:save!)
+ changed_fields.each { |f| f.marked_for_destruction? ? f.destroy : f.save! }
changed_fields.clear
theme_modifier_set.save!
+ theme_fields.reload
+
theme_fields.select(&:basic_html_field?).each(&:invalidate_baked!) if saved_change_to_name?
if saved_change_to_color_scheme_id? || saved_change_to_dark_color_scheme_id? ||
@@ -169,7 +172,7 @@ class Theme < ActiveRecord::Base
settings_field&.ensure_baked! # Other fields require setting to be **baked**
theme_fields.each(&:ensure_baked!)
- update_javascript_cache!
+ update_javascript_cache! if any_extra_js_fields_changed
remove_from_cache!
ColorScheme.hex_cache.clear
@@ -214,7 +217,7 @@ class Theme < ActiveRecord::Base
def update_javascript_cache!
all_extra_js = load_all_extra_js
if all_extra_js.present?
- js_compiler = ThemeJavascriptCompiler.new(id, name, build_settings_hash)
+ js_compiler = ThemeJavascriptCompiler.new(id, name)
js_compiler.append_tree(all_extra_js)
javascript_cache || build_javascript_cache
@@ -682,7 +685,8 @@ class Theme < ActiveRecord::Base
if field
if value.blank? && !upload_id
- field.destroy
+ field.mark_for_destruction
+ changed_fields << field
else
if field.value != value || field.upload_id != upload_id
field.value = value
@@ -1020,7 +1024,6 @@ class Theme < ActiveRecord::Base
end
reload
- update_javascript_cache!
end
if start_transaction
@@ -1066,7 +1069,7 @@ class Theme < ActiveRecord::Base
theme_fields.where(target_id: Theme.targets[:migrations]).order(name: :asc),
)
- compiler = ThemeJavascriptCompiler.new(id, name, cached_default_settings, minify: false)
+ compiler = ThemeJavascriptCompiler.new(id, name, minify: false)
compiler.append_tree(load_all_extra_js)
compiler.append_tree(migrations_tree)
compiler.append_tree(tests_tree)
diff --git a/app/views/qunit/qunit.html.erb b/app/views/qunit/qunit.html.erb
index 630247fc48f..ca1b8065b2e 100644
--- a/app/views/qunit/qunit.html.erb
+++ b/app/views/qunit/qunit.html.erb
@@ -37,6 +37,7 @@
<%= theme_lookup("head_tag") %>
<%= theme_tests %>
+ <%= fake_preload_data %>
<%= tag.meta id: 'data-discourse-setup', data: client_side_setup_data %>
<meta property="og:title" content="">
diff --git a/frontend/asset-processor/rollup-virtual-imports.js b/frontend/asset-processor/rollup-virtual-imports.js
index 870f77329b3..e50cfc0b28a 100644
--- a/frontend/asset-processor/rollup-virtual-imports.js
+++ b/frontend/asset-processor/rollup-virtual-imports.js
@@ -12,12 +12,6 @@ export default {
) => {
let output = `const compatModules = {};`;
- if (themeId) {
- output += cleanMultiline(`
- import "virtual:init-settings";
- `);
- }
-
const moduleFilenamesSet = new Set(moduleFilenames);
const exportedModules = new Set();
@@ -102,12 +96,6 @@ export default {
return output;
},
- "virtual:init-settings": ({ themeId, settings }) => {
- return (
- `import { registerSettings } from "discourse/lib/theme-settings-store";\n\n` +
- `registerSettings(${themeId}, ${JSON.stringify(settings, null, 2)});\n`
- );
- },
"virtual:theme": ({ themeId }) => {
return cleanMultiline(`
import { getObjectForTheme } from "discourse/lib/theme-settings-store";
diff --git a/frontend/discourse/app/app.js b/frontend/discourse/app/app.js
index 043c8a40382..7ee6d5842c2 100644
--- a/frontend/discourse/app/app.js
+++ b/frontend/discourse/app/app.js
@@ -8,6 +8,7 @@ import "./module-shims";
import "./discourse-common-loader-shims";
import embroiderCompatModules from "@embroider/virtual/compat-modules";
import { registerDiscourseImplicitInjections } from "discourse/lib/implicit-injections";
+import { registerSettings } from "discourse/lib/theme-settings-store";
import { defineModules } from "./lib/loader-shim";
// Register Discourse's standard implicit injections on common framework classes.
@@ -100,7 +101,23 @@ async function loadPluginFromModulePreload(link) {
}
}
+function registerPreloadedThemeSettings() {
+ try {
+ const element = document.getElementById("data-preloaded");
+ const preloaded = JSON.parse(element.dataset.preloaded);
+ const activatedThemes = JSON.parse(preloaded.activatedThemes);
+ for (const [themeId, info] of Object.entries(activatedThemes)) {
+ registerSettings(parseInt(themeId, 10), info.settings);
+ }
+ } catch (error) {
+ // eslint-disable-next-line no-console
+ console.error("Failed to register preloaded theme settings", error);
+ }
+}
+
export async function loadThemesAndPlugins() {
+ registerPreloadedThemeSettings();
+
const promises = [
...[
...document.querySelectorAll("link[rel=modulepreload][data-theme-id]"),
diff --git a/frontend/discourse/app/lib/source-identifier.js b/frontend/discourse/app/lib/source-identifier.js
index 5faa1749ea6..d9d50130c7c 100644
--- a/frontend/discourse/app/lib/source-identifier.js
+++ b/frontend/discourse/app/lib/source-identifier.js
@@ -58,7 +58,8 @@ export default function identifySource(error) {
}
export function getThemeInfo(id) {
- const name = PreloadStore.get("activatedThemes")?.[id] || `(theme-id: ${id})`;
+ const name =
+ PreloadStore.get("activatedThemes")?.[id]?.name || `(theme-id: ${id})`;
return {
id,
name,
diff --git a/lib/application_layout_preloader.rb b/lib/application_layout_preloader.rb
index b149a8c4abb..66c6eb930df 100644
--- a/lib/application_layout_preloader.rb
+++ b/lib/application_layout_preloader.rb
@@ -136,7 +136,12 @@ class ApplicationLayoutPreloader
id = @theme_id
return "{}" if id.blank?
ids = Theme.transform_ids(id)
- Theme.where(id: ids).pluck(:id, :name).to_h.to_json
+ Theme
+ .where(id: ids)
+ .each_with_object({}) do |theme, hash|
+ hash[theme.id] = { name: theme.name, settings: theme.cached_settings }
+ end
+ .to_json
end
def load_font_map
diff --git a/lib/asset_processor.rb b/lib/asset_processor.rb
index df2803b343d..15a79a1b105 100644
--- a/lib/asset_processor.rb
+++ b/lib/asset_processor.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class AssetProcessor
- BASE_COMPILER_VERSION = 108
+ BASE_COMPILER_VERSION = 109
PROCESSOR_DIR = "tmp/asset-processor"
LOCK_FILE = "#{PROCESSOR_DIR}/build.lock"
diff --git a/lib/theme_javascript_compiler.rb b/lib/theme_javascript_compiler.rb
index f9f6a0c1c88..78d2f93494e 100644
--- a/lib/theme_javascript_compiler.rb
+++ b/lib/theme_javascript_compiler.rb
@@ -15,12 +15,11 @@ class ThemeJavascriptCompiler
@@terser_disabled = false
end
- def initialize(theme_id, theme_name, settings = {}, minify: true)
+ def initialize(theme_id, theme_name, minify: true)
@theme_id = theme_id
@input_tree = {}
@theme_name = theme_name
@minify = minify
- @settings = settings
end
def compile!
@@ -41,7 +40,6 @@ class ThemeJavascriptCompiler
@input_tree,
{
themeId: @theme_id,
- settings: @settings,
minify: @minify && !@@terser_disabled,
entrypoints: {
main: {
diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rb
index 2e229caea72..2488bc7963c 100644
--- a/spec/lib/discourse_spec.rb
+++ b/spec/lib/discourse_spec.rb
@@ -690,18 +690,12 @@ RSpec.describe Discourse do
)
end
- it "invalidates all JS and CSS caches" do
+ it "invalidates all theme settings and CSS caches" do
Stylesheet::Manager.clear_theme_cache!
old_upload_url = Discourse.store.cdn_url(upload.url)
- js_file_script =
- Nokogiri::HTML5
- .fragment(Theme.lookup_field(theme.id, :extra_js, nil))
- .css("link[rel=modulepreload]")
- .first
- file_js = JavascriptCache.find_by(digest: js_file_script[:href][/\h{40}/]).content
- expect(file_js).to include(old_upload_url)
+ expect(theme.cached_settings["theme_uploads"]["imajee"]).to eq(old_upload_url)
css_link_tag =
Nokogiri::HTML5
@@ -716,13 +710,7 @@ RSpec.describe Discourse do
SiteSetting.s3_cdn_url = "https://new.s3.cdn.com/gg"
new_upload_url = Discourse.store.cdn_url(upload.url)
- js_file_script =
- Nokogiri::HTML5
- .fragment(Theme.lookup_field(theme.id, :extra_js, nil))
- .css("link[rel=modulepreload]")
- .first
- file_js = JavascriptCache.find_by(digest: js_file_script[:href][/\h{40}/]).content
- expect(file_js).to include(old_upload_url)
+ expect(theme.cached_settings["theme_uploads"]["imajee"]).to eq(old_upload_url)
css_link_tag =
Nokogiri::HTML5
@@ -736,13 +724,7 @@ RSpec.describe Discourse do
Discourse.clear_all_theme_cache!
- js_file_script =
- Nokogiri::HTML5
- .fragment(Theme.lookup_field(theme.id, :extra_js, nil))
- .css("link[rel=modulepreload]")
- .first
- file_js = JavascriptCache.find_by(digest: js_file_script[:href][/\h{40}/]).content
- expect(file_js).to include(new_upload_url)
+ expect(theme.cached_settings["theme_uploads"]["imajee"]).to eq(new_upload_url)
css_link_tag =
Nokogiri::HTML5
diff --git a/spec/lib/theme_javascript_compiler_spec.rb b/spec/lib/theme_javascript_compiler_spec.rb
index e631488fd04..037c140e874 100644
--- a/spec/lib/theme_javascript_compiler_spec.rb
+++ b/spec/lib/theme_javascript_compiler_spec.rb
@@ -172,7 +172,7 @@ RSpec.describe ThemeJavascriptCompiler do
end
describe "terser compilation" do
- let(:compiler) { ThemeJavascriptCompiler.new(1, "marks", {}, minify: true) }
+ let(:compiler) { ThemeJavascriptCompiler.new(1, "marks", minify: true) }
it "applies terser and provides sourcemaps" do
sources = {
diff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rb
index f0d5eb3a53e..04e8ac3fa6a 100644
--- a/spec/models/theme_field_spec.rb
+++ b/spec/models/theme_field_spec.rb
@@ -224,7 +224,6 @@ RSpec.describe ThemeField do
expect(theme.javascript_cache.content).to include(
'compatModules["discourse/controllers/discovery-2"]',
)
- expect(theme.javascript_cache.content).to include("registerSettings(")
expect(theme.javascript_cache.content).to include(
"[THEME #{theme.id}] Unsupported file type: discourse/controllers/discovery.blah",
)
@@ -241,9 +240,8 @@ RSpec.describe ThemeField do
"theme-#{theme.id}/discourse/templates/discovery.hbs",
"theme-#{theme.id}/virtual:entrypoint:main",
"theme-#{theme.id}/virtual:theme",
- "theme-#{theme.id}/virtual:init-settings",
)
- expect(map["sourcesContent"].length).to eq(6)
+ expect(map["sourcesContent"].length).to eq(5)
end
def create_upload_theme_field!(name)
@@ -281,26 +279,6 @@ RSpec.describe ThemeField do
let(:key) { "themes.settings_errors" }
- it "forces re-transpilation of theme JS when settings YAML changes" do
- theme.set_field(target: :settings, name: "yaml", value: "setting: 5")
- theme.set_field(
- target: :extra_js,
- name: "discourse/initializers/my-initializer.js",
- value: "console.log(settings.setting);",
- )
- theme.save!
-
- old_js_cache_content = theme.reload.javascript_cache.content
- expect(old_js_cache_content).to include('"setting": 5')
-
- theme.set_field(target: :settings, name: "yaml", value: "setting: 66")
- theme.save!
-
- new_js_cache_content = theme.reload.javascript_cache.content
- expect(new_js_cache_content).to include('"setting": 66')
- expect(new_js_cache_content).not_to include('"setting": 5')
- end
-
it "generates errors for bad YAML" do
yaml = "invalid_setting 5"
field = create_yaml_field(yaml)
@@ -746,17 +724,15 @@ RSpec.describe ThemeField do
expected_local_js_cache_url = js_field.javascript_cache.local_url
expect(expected_local_js_cache_url).to start_with("/theme-javascripts/")
- expect(theme.reload.javascript_cache.content).to include(<<~JS)
- registerSettings(#{theme.id}, {
- "hello": "world",
- "theme_uploads": {
- "test_js": "#{js_field.upload.url}"
- },
- "theme_uploads_local": {
- "test_js": "#{js_field.javascript_cache.local_url}"
- }
- });
- JS
+ expect(theme.reload.cached_settings).to include(
+ :hello => "world",
+ "theme_uploads" => {
+ "test_js" => js_field.upload.url,
+ },
+ "theme_uploads_local" => {
+ "test_js" => js_field.javascript_cache.local_url,
+ },
+ )
# this is important, we do not want local_js_urls to leak into scss
expect(theme.scss_variables).to include("$hello: unquote(\"world\");")
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index ca8a479417c..ae254db92a2 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -316,12 +316,9 @@ RSpec.describe Theme do
)
theme.save!
+ expect(theme.cached_settings).to include(name: "bob")
+
javascript_cache = theme.reload.javascript_cache
- expect(javascript_cache.content).to include <<~JS
- registerSettings(#{theme.id}, {
- "name": "bob"
- });
- JS
expect(javascript_cache.content).to include("alert(settings.name)")
expect(javascript_cache.content).to include("let a = () => {}")
@@ -329,11 +326,7 @@ RSpec.describe Theme do
setting.value = "bill"
theme.save!
- expect(theme.reload.javascript_cache.content).to include <<~JS
- registerSettings(#{theme.id}, {
- "name": "bill"
- });
- JS
+ expect(theme.reload.cached_settings).to include(name: "bill")
end
it "is empty when the settings are invalid" do
@@ -1025,11 +1018,11 @@ RSpec.describe Theme do
)
end
- it "updates the theme's javascript cache after running migration" do
+ it "updates the theme settings after running migration" do
theme.set_field(target: :extra_js, name: "test.js.es6", value: "const hello = 'world';")
theme.save!
- expect(theme.javascript_cache.content).to include('"list_setting": "aa,bb"')
+ expect(theme.settings[:list_setting].value).to eq("aa,bb")
settings_field.update!(value: <<~YAML)
integer_setting: 1
@@ -1052,7 +1045,6 @@ RSpec.describe Theme do
expect(setting_record.data_type).to eq(ThemeSetting.types[:list])
expect(setting_record.value).to eq("zz|aa")
- expect(theme.javascript_cache.content).to include('"list_setting": "zz|aa"')
end
it "allows changing a setting's type" do
diff --git a/spec/requests/theme_javascripts_controller_spec.rb b/spec/requests/theme_javascripts_controller_spec.rb
index 4fb55b6c14e..ae445e4b099 100644
--- a/spec/requests/theme_javascripts_controller_spec.rb
+++ b/spec/requests/theme_javascripts_controller_spec.rb
@@ -137,53 +137,11 @@ RSpec.describe ThemeJavascriptsController do
component.save!
end
- it "sets theme settings default values" do
- component.update_setting(:num_setting, 643)
+ it "serves the bundled test file" do
_, digest = component.baked_js_tests_with_digest
get "/theme-javascripts/tests/#{component.id}-#{digest}.js"
- expect(response.body).to include <<~JS
- registerSettings(#{component.id}, {
- "num_setting": 5
- });
- JS
- expect(response.body).to include("assert.ok(true);")
- end
-
- it "includes theme uploads URLs in the settings object" do
- SiteSetting.authorized_extensions = "*"
- js_file = Tempfile.new(%w[vendorlib .js])
- js_file.write("console.log(123);\n")
- js_file.rewind
- js_upload = UploadCreator.new(js_file, "vendorlib.js").create_for(Discourse::SYSTEM_USER_ID)
- component.set_field(
- type: :theme_upload_var,
- target: :common,
- name: "vendorlib",
- upload_id: js_upload.id,
- )
- component.save!
- _, digest = component.baked_js_tests_with_digest
-
- theme_javascript_hash =
- component.theme_fields.find_by(upload_id: js_upload.id).javascript_cache.digest
-
- get "/theme-javascripts/tests/#{component.id}-#{digest}.js"
- expect(response.body).to include <<~JS
- registerSettings(#{component.id}, {
- "num_setting": 5,
- "theme_uploads": {
- "vendorlib": "/uploads/default/test_#{Discourse.test_env_number}/original/1X/#{js_upload.sha1}.js"
- },
- "theme_uploads_local": {
- "vendorlib": "/theme-javascripts/#{theme_javascript_hash}.js?__ws=test.localhost"
- }
- });
- JS
expect(response.body).to include("assert.ok(true);")
- ensure
- js_file&.close
- js_file&.unlink
end
it "responds with 404 if digest is not a 40 chars hex" do