Skip to content

A platform for community discussion. Free, open, simple.

FIX: cache admin locale file for 24 hours

Sam8ff5f5f2efa6
diff --git a/app/controllers/extra_locales_controller.rb b/app/controllers/extra_locales_controller.rb
index bc8cf8c4758..d0e4884fcc4 100644
--- a/app/controllers/extra_locales_controller.rb
+++ b/app/controllers/extra_locales_controller.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
class ExtraLocalesController < ApplicationController
layout :false
@@ -6,7 +8,29 @@ class ExtraLocalesController < ApplicationController
def show
bundle = params[:bundle]
raise Discourse::InvalidAccess.new unless bundle =~ /^(admin|wizard)$/
+ if params[:v] && params[:v].length == 32
+ hash = ExtraLocalesController.bundle_js_hash(bundle)
+ if hash == params[:v]
+ immutable_for 24.hours
+ end
+ end
+ render plain: ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript"
+ end
+
+ def self.bundle_js_hash(bundle)
+ @bundle_js_hash ||= {}
+ @bundle_js_hash[bundle] = Digest::MD5.hexdigest(bundle_js(bundle))
+ end
+
+ def self.url(bundle)
+ if Rails.env == "production"
+ "#{Discourse.base_uri}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
+ else
+ "#{Discourse.base_uri}/extra-locales/#{bundle}"
+ end
+ end
+ def self.bundle_js(bundle)
locale_str = I18n.locale.to_s
bundle_str = "#{bundle}_js"
@@ -32,6 +56,6 @@ class ExtraLocalesController < ApplicationController
JS
end
- render plain: js, content_type: "application/javascript"
+ js
end
end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 4eb4c3f275e..b597a3217c2 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -36,7 +36,7 @@
<%- end %>
<%- if staff? %>
- <script src="<%= Discourse.base_uri %>/extra-locales/admin"></script>
+ <script src="<%= ExtraLocalesController.url('admin') %>"></script>
<%= preload_script "admin" %>
<%- end %>
diff --git a/app/views/wizard/qunit.html.erb b/app/views/wizard/qunit.html.erb
index 4e6e7a3a59f..561a2ade044 100644
--- a/app/views/wizard/qunit.html.erb
+++ b/app/views/wizard/qunit.html.erb
@@ -8,7 +8,7 @@
<%= javascript_include_tag "qunit" %>
<%= javascript_include_tag "wizard/test/test_helper" %>
<%= csrf_meta_tags %>
- <script src="/extra-locales/wizard"></script>
+ <script src="<%= ExtraLocalesController.url("wizard") %>"></script>
</head>
<body>
<div id="qunit"></div>
diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb
index 4d1b15e8a5a..59a9c27c3cb 100644
--- a/lib/js_locale_helper.rb
+++ b/lib/js_locale_helper.rb
@@ -131,9 +131,9 @@ module JsLocaleHelper
message_formats.merge!(strip_out_message_formats!(translations[locale_str]['admin_js']))
result = generate_message_format(message_formats, locale_str)
- translations.keys.each do |locale|
- translations[locale].keys.each do |k|
- translations[locale].delete(k) unless k == "js"
+ translations.keys.each do |l|
+ translations[l].keys.each do |k|
+ translations[l].delete(k) unless k == "js"
end
end
diff --git a/spec/controllers/extra_locales_controller_spec.rb b/spec/controllers/extra_locales_controller_spec.rb
index 8ec2784ead2..c3aa308d517 100644
--- a/spec/controllers/extra_locales_controller_spec.rb
+++ b/spec/controllers/extra_locales_controller_spec.rb
@@ -4,6 +4,16 @@ describe ExtraLocalesController do
context 'show' do
+ it "caches for 24 hours if version is provided and it matches current hash" do
+ get :show, params: { bundle: 'admin', v: ExtraLocalesController.bundle_js_hash('admin') }
+ expect(response.headers["Cache-Control"]).to eq("max-age=86400, public, immutable")
+ end
+
+ it "does not cache at all if version is invalid" do
+ get :show, params: { bundle: 'admin', v: 'a' * 32 }
+ expect(response.headers["Cache-Control"]).not_to eq("max-age=86400, public, immutable")
+ end
+
it "needs a valid bundle" do
get :show, params: { bundle: 'made-up-bundle' }
expect(response).to_not be_success