From 0f1afad6dab73ab9d862fcbc05f1387d98222a28 Mon Sep 17 00:00:00 2001 From: Kyle Zhao Date: Thu, 18 Oct 2018 02:05:34 -0400 Subject: [PATCH] FIX: extracted theme JavaScripts for multisite (#6502) * FIX: extracted theme javascripts for multisite * onceoff to rebake all theme fields --- app/jobs/onceoff/rebake_all_html_theme_fields.rb | 11 +++++++++++ app/models/javascript_cache.rb | 2 +- spec/jobs/rebake_all_html_theme_fields_spec.rb | 15 +++++++++++++++ spec/models/javascript_cache_spec.rb | 7 +++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 app/jobs/onceoff/rebake_all_html_theme_fields.rb create mode 100644 spec/jobs/rebake_all_html_theme_fields_spec.rb diff --git a/app/jobs/onceoff/rebake_all_html_theme_fields.rb b/app/jobs/onceoff/rebake_all_html_theme_fields.rb new file mode 100644 index 00000000000..afdce8abc11 --- /dev/null +++ b/app/jobs/onceoff/rebake_all_html_theme_fields.rb @@ -0,0 +1,11 @@ +module Jobs + class RebakeAllHtmlThemeFields < Jobs::Onceoff + def execute_onceoff(args) + ThemeField.where(type_id: ThemeField.types[:html]).find_each do |theme_field| + theme_field.update(value_baked: nil) + end + + Theme.clear_cache! + end + end +end diff --git a/app/models/javascript_cache.rb b/app/models/javascript_cache.rb index e11c40feb29..cb850af6de1 100644 --- a/app/models/javascript_cache.rb +++ b/app/models/javascript_cache.rb @@ -7,7 +7,7 @@ class JavascriptCache < ActiveRecord::Base before_save :update_digest def url - "#{GlobalSetting.cdn_url}#{GlobalSetting.relative_url_root}/theme-javascripts/#{digest}.js" + "#{GlobalSetting.cdn_url}#{GlobalSetting.relative_url_root}/theme-javascripts/#{digest}.js?__ws=#{Discourse.current_hostname}" end private diff --git a/spec/jobs/rebake_all_html_theme_fields_spec.rb b/spec/jobs/rebake_all_html_theme_fields_spec.rb new file mode 100644 index 00000000000..91dd4828f76 --- /dev/null +++ b/spec/jobs/rebake_all_html_theme_fields_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +describe Jobs::RebakeAllHtmlThemeFields do + let(:theme) { Fabricate(:theme) } + let(:theme_field) { ThemeField.create!(theme: theme, target_id: 0, name: "header", value: "") } + + it 'extracts inline javascripts' do + theme_field.update_attributes(value_baked: 'need to be rebaked') + + described_class.new.execute_onceoff({}) + + theme_field.reload + expect(theme_field.value_baked).to include('theme-javascripts') + end +end diff --git a/spec/models/javascript_cache_spec.rb b/spec/models/javascript_cache_spec.rb index 1599fbe92e6..28b06a2ea8c 100644 --- a/spec/models/javascript_cache_spec.rb +++ b/spec/models/javascript_cache_spec.rb @@ -29,4 +29,11 @@ RSpec.describe JavascriptCache, type: :model do expect(javascript_cache.errors.details[:content]).to include(error: :empty) end end + + describe 'url' do + it 'works with multisite' do + javascript_cache = JavascriptCache.create!(content: 'console.log("hello");', theme_field: theme_field) + expect(javascript_cache.url).to include("?__ws=test.localhost") + end + end end