Skip to content

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

FIX: Correctly clear theme stylesheet cache when changing color scheme

David Taylore84531a6a654
diff --git a/app/models/theme.rb b/app/models/theme.rb
index a96ef39f23c..74b104946d2 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -52,6 +52,7 @@ class Theme < ActiveRecord::Base
changed_fields.clear
Theme.expire_site_cache! if saved_change_to_user_selectable? || saved_change_to_name?
+ notify_with_scheme = saved_change_to_color_scheme_id?
reload
settings_field&.ensure_baked! # Other fields require setting to be **baked**
@@ -60,6 +61,7 @@ class Theme < ActiveRecord::Base
remove_from_cache!
clear_cached_settings!
ColorScheme.hex_cache.clear
+ notify_theme_change(with_scheme: notify_with_scheme)
end
after_destroy do
@@ -86,10 +88,6 @@ class Theme < ActiveRecord::Base
SvgSprite.expire_cache
end
- after_commit ->(theme) do
- theme.notify_theme_change(with_scheme: theme.saved_change_to_color_scheme_id?)
- end, on: [:create, :update]
-
def self.get_set_cache(key, &blk)
if val = @cache[key]
return val
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index 9d8fa631f69..d145540ae6e 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -481,6 +481,31 @@ HTML
expect(ColorScheme.hex_for_name('header_primary')).to eq('333333')
end
+ it "correctly notifies about theme changes" do
+ cs1 = Fabricate(:color_scheme)
+ cs2 = Fabricate(:color_scheme)
+
+ theme = Fabricate(:theme,
+ user_selectable: true,
+ user: user,
+ color_scheme_id: cs1.id
+ )
+
+ messages = MessageBus.track_publish do
+ theme.save!
+ end.filter { |m| m.channel == "/file-change" }
+ expect(messages.count).to eq(1)
+ expect(messages.first.data.map { |d| d[:target] }).to contain_exactly(:desktop_theme, :mobile_theme)
+
+ # With color scheme change:
+ messages = MessageBus.track_publish do
+ theme.color_scheme_id = cs2.id
+ theme.save!
+ end.filter { |m| m.channel == "/file-change" }
+ expect(messages.count).to eq(1)
+ expect(messages.first.data.map { |d| d[:target] }).to contain_exactly(:admin, :desktop, :desktop_theme, :mobile, :mobile_theme)
+ end
+
it 'handles settings cache correctly' do
Theme.destroy_all