Extend custom CSS cache time with digest paths (#33207)

This commit is contained in:
Matt Jankowski 2025-01-08 03:48:45 -05:00 committed by GitHub
parent b3243ef41c
commit c0264c8013
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 142 additions and 6 deletions

View file

@ -2,7 +2,7 @@
class CustomCssController < ActionController::Base # rubocop:disable Rails/ApplicationController
def show
expires_in 3.minutes, public: true
expires_in 1.month, public: true
render content_type: 'text/css'
end

View file

@ -23,8 +23,31 @@ module ThemeHelper
end
end
def custom_stylesheet
if active_custom_stylesheet.present?
stylesheet_link_tag(
custom_css_path(active_custom_stylesheet),
host: root_url,
media: :all,
skip_pipeline: true
)
end
end
private
def active_custom_stylesheet
if cached_custom_css_digest.present?
[:custom, cached_custom_css_digest.to_s.first(8)]
.compact_blank
.join('-')
end
end
def cached_custom_css_digest
Rails.cache.read(:setting_digest_custom_css)
end
def theme_color_for(theme)
theme == 'mastodon-light' ? Themes::THEME_COLORS[:light] : Themes::THEME_COLORS[:dark]
end

View file

@ -69,6 +69,10 @@ class Form::AdminSettings
favicon
).freeze
DIGEST_KEYS = %i(
custom_css
).freeze
OVERRIDEN_SETTINGS = {
authorized_fetch: :authorized_fetch_mode?,
}.freeze
@ -122,6 +126,8 @@ class Form::AdminSettings
KEYS.each do |key|
next unless instance_variable_defined?(:"@#{key}")
cache_digest_value(key) if DIGEST_KEYS.include?(key)
if UPLOAD_KEYS.include?(key)
public_send(key).save
else
@ -133,6 +139,18 @@ class Form::AdminSettings
private
def cache_digest_value(key)
Rails.cache.delete(:"setting_digest_#{key}")
key_value = instance_variable_get(:"@#{key}")
if key_value.present?
Rails.cache.write(
:"setting_digest_#{key}",
Digest::SHA256.hexdigest(key_value)
)
end
end
def typecast_value(key, value)
if BOOLEAN_KEYS.include?(key)
value == '1'

View file

@ -34,7 +34,7 @@
= csrf_meta_tags unless skip_csrf_meta_tags?
%meta{ name: 'style-nonce', content: request.content_security_policy_nonce }
= stylesheet_link_tag custom_css_path, skip_pipeline: true, host: root_url, media: 'all'
= custom_stylesheet
= yield :header_tags