FIX: cache admin locale file for 24 hours
This commit is contained in:
parent
fb863e18ec
commit
8ff5f5f2ef
|
@ -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
|
||||
|
|
|
@ -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 %>
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue