A platform for community discussion. Free, open, simple.
DEV: Don't create duplicate stylesheet cache entries (#38240)
Jarek Radosz87f3951f6dff
diff --git a/app/controllers/stylesheets_controller.rb b/app/controllers/stylesheets_controller.rbindex a1a077eec0d..a65ae6fa1e9 100644--- a/app/controllers/stylesheets_controller.rb+++ b/app/controllers/stylesheets_controller.rb@@ -24,7 +24,7 @@ class StylesheetsController < ApplicationControllerparams.require("id")params.permit("theme_id")- manager = Stylesheet::Manager.new(theme_id: params[:theme_id])+ manager = Stylesheet::Manager.new(theme_id: params[:theme_id]&.to_i)stylesheet = manager.color_scheme_stylesheet_details(params[:id], fallback_to_base: true)render json: stylesheetdiff --git a/config/routes.rb b/config/routes.rbindex efe597dd28a..dc08e0333ca 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1078,6 +1078,7 @@ Discourse::Application.routes.draw doget "color-scheme-stylesheet/:id(/:theme_id)" => "stylesheets#color_scheme",:constraints => {format: :json,+ theme_id: /-?\d+/,}get "theme-javascripts/:digest" => "theme_javascripts#show",:constraints => {diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rbindex 637bc1746a4..31fd678a04c 100644--- a/lib/stylesheet/manager.rb+++ b/lib/stylesheet/manager.rb@@ -341,35 +341,23 @@ class Stylesheet::Managerdef color_scheme_stylesheet_details(color_scheme_id = nil, fallback_to_base: true)theme_id = @theme_id || SiteSetting.default_theme_id+ theme_id = nil if !Theme.theme_ids.include?(theme_id)color_scheme = ColorScheme.find_by(id: color_scheme_id)+ return if !color_scheme && !fallback_to_base- if !color_scheme- return if !fallback_to_base- color_scheme = get_theme(theme_id)&.color_scheme || ColorScheme.base- end-- target = COLOR_SCHEME_STYLESHEET.to_sym- current_hostname = Discourse.current_hostname+ theme = theme_id ? get_theme(theme_id) : nil+ color_scheme ||= theme&.color_scheme || ColorScheme.basecache_key = self.class.color_scheme_cache_key(color_scheme, theme_id)cache.defer_get_set(cache_key) dostylesheet = { color_scheme_id: color_scheme.id }- theme = get_theme(theme_id)-builder =- Builder.new(- target: target,- theme: get_theme(theme_id),- color_scheme: color_scheme,- manager: self,- )-+ Builder.new(target: COLOR_SCHEME_STYLESHEET.to_sym, theme:, color_scheme:, manager: self)builder.compile unless File.exist?(builder.stylesheet_fullpath)- href = builder.stylesheet_absolute_url- stylesheet[:new_href] = href+ stylesheet[:new_href] = builder.stylesheet_absolute_urlstylesheet.freezeenddiff --git a/spec/requests/stylesheets_controller_spec.rb b/spec/requests/stylesheets_controller_spec.rbindex 91ee070a113..3063e278426 100644--- a/spec/requests/stylesheets_controller_spec.rb+++ b/spec/requests/stylesheets_controller_spec.rb@@ -159,5 +159,21 @@ RSpec.describe StylesheetsController dojson = JSON.parse(response.body)expect(json["color_scheme_id"]).to eq(scheme.id)end++ it "does not create duplicate cache entries for incorrect theme_ids" do+ scheme = ColorScheme.create_from_base(name: "test scheme", colors: [])+ incorrect_theme_id = Theme.maximum(:id).to_i + 100++ Stylesheet::Manager.cache.clear+ expect(Stylesheet::Manager.cache.hash.size).to eq(0)++ get "/color-scheme-stylesheet/#{scheme.id}/#{incorrect_theme_id}.json"+ expect(response.status).to eq(200)+ expect(Stylesheet::Manager.cache.hash.size).to eq(1)++ get "/color-scheme-stylesheet/#{scheme.id}/#{incorrect_theme_id + 1}.json"+ expect(response.status).to eq(200)+ expect(Stylesheet::Manager.cache.hash.size).to eq(1)+ endendend