Fix custom css cache miss (#33583)

This commit is contained in:
Jeong Arm 2025-01-14 17:24:00 +09:00 committed by GitHub
parent 77a44e61a8
commit 74da9e9281
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 4 deletions

View File

@ -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)

View File

@ -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