diff --git a/app/helpers/theme_helper.rb b/app/helpers/theme_helper.rb index 5dfb4a5184c..cda380b3bc0 100644 --- a/app/helpers/theme_helper.rb +++ b/app/helpers/theme_helper.rb @@ -45,7 +45,9 @@ module ThemeHelper end def cached_custom_css_digest - Rails.cache.read(:setting_digest_custom_css) + Rails.cache.fetch(:setting_digest_custom_css) do + Setting.custom_css&.then { |content| Digest::SHA256.hexdigest(content) } + end end def theme_color_for(theme) diff --git a/spec/helpers/theme_helper_spec.rb b/spec/helpers/theme_helper_spec.rb index c811b7c9811..51611b82112 100644 --- a/spec/helpers/theme_helper_spec.rb +++ b/spec/helpers/theme_helper_spec.rb @@ -80,18 +80,37 @@ RSpec.describe ThemeHelper do end describe '#custom_stylesheet' do + let(:custom_css) { 'body {}' } + let(:custom_digest) { Digest::SHA256.hexdigest(custom_css) } + + before do + Setting.custom_css = custom_css + end + context 'when custom css setting value digest is present' do - before { Rails.cache.write(:setting_digest_custom_css, '1a2s3d4f1a2s3d4f') } + before { Rails.cache.write(:setting_digest_custom_css, custom_digest) } it 'returns value from settings' do expect(custom_stylesheet) - .to match('/css/custom-1a2s3d4f.css') + .to match("/css/custom-#{custom_digest[...8]}.css") end end - context 'when custom css setting value digest is not present' do + context 'when custom css setting value digest is expired' do before { Rails.cache.delete(:setting_digest_custom_css) } + it 'returns value from settings' do + expect(custom_stylesheet) + .to match("/css/custom-#{custom_digest[...8]}.css") + end + end + + context 'when custom css setting is not present' do + before do + Setting.custom_css = nil + Rails.cache.delete(:setting_digest_custom_css) + end + it 'returns default value' do expect(custom_stylesheet) .to be_blank