From 63c90e8c5992b23f153f7f1c60445ff3c750741f Mon Sep 17 00:00:00 2001 From: Amanda Alves Branquinho Date: Tue, 2 Jul 2024 16:43:08 -0300 Subject: [PATCH] PERF: eager load field changes (#27675) Fix N+1 issue for javascript_cache stable backport for #27626 --- app/models/theme.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models/theme.rb b/app/models/theme.rb index f3cadd0394f..f0758432b7b 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -697,17 +697,21 @@ class Theme < ActiveRecord::Base def build_theme_uploads_hash hash = {} - upload_fields.each do |field| - hash[field.name] = Discourse.store.cdn_url(field.upload.url) if field.upload&.url - end + upload_fields + .includes(:javascript_cache, :upload) + .each do |field| + hash[field.name] = Discourse.store.cdn_url(field.upload.url) if field.upload&.url + end hash end def build_local_theme_uploads_hash hash = {} - upload_fields.each do |field| - hash[field.name] = field.javascript_cache.local_url if field.javascript_cache - end + upload_fields + .includes(:javascript_cache, :upload) + .each do |field| + hash[field.name] = field.javascript_cache.local_url if field.javascript_cache + end hash end @@ -826,6 +830,8 @@ class Theme < ActiveRecord::Base if upload = field.upload url = upload_cdn_path(upload.url) contents << "$#{field.name}: unquote(\"#{url}\");" + else + contents << "$#{field.name}: unquote(\"\");" end else contents << to_scss_variable(field.name, field.value)