A platform for community discussion. Free, open, simple.
PERF: Move extra-locale digest from query-param to path (#31479)
David Taylor12291f745c3f
diff --git a/app/assets/javascripts/discourse/tests/index.html b/app/assets/javascripts/discourse/tests/index.htmlindex 3d88c4a1340..3582ff67394 100644--- a/app/assets/javascripts/discourse/tests/index.html+++ b/app/assets/javascripts/discourse/tests/index.html@@ -54,9 +54,9 @@<script src="{{rootURL}}assets/discourse.js"></script><script src="{{rootURL}}assets/locales/en.js" data-embroider-ignore></script>- <script src="{{rootURL}}extra-locales/mf" data-embroider-ignore></script>- <script src="{{rootURL}}extra-locales/admin" data-embroider-ignore></script>- <script src="{{rootURL}}extra-locales/wizard" data-embroider-ignore></script>+ <script src="{{rootURL}}extra-locales/0000000000000000000000000000000000000000/mf" data-embroider-ignore></script>+ <script src="{{rootURL}}extra-locales/0000000000000000000000000000000000000000/admin" data-embroider-ignore></script>+ <script src="{{rootURL}}extra-locales/0000000000000000000000000000000000000000/wizard" data-embroider-ignore></script><script src="{{rootURL}}assets/test-site-settings.js" data-embroider-ignore></script><script src="{{rootURL}}assets/admin.js" data-embroider-ignore></script>diff --git a/app/controllers/extra_locales_controller.rb b/app/controllers/extra_locales_controller.rbindex 367dd221b7b..d120fe3f3da 100644--- a/app/controllers/extra_locales_controller.rb+++ b/app/controllers/extra_locales_controller.rb@@ -10,7 +10,7 @@ class ExtraLocalesController < ApplicationController:verify_authenticity_tokenOVERRIDES_BUNDLE = "overrides"- MD5_HASH_LENGTH = 32+ SHA1_HASH_LENGTH = 40MF_BUNDLE = "mf"ADMIN_BUNDLE = "admin"WIZARD_BUNDLE = "wizard"@@ -31,17 +31,20 @@ class ExtraLocalesController < ApplicationControllerjs_digests[:site_specific][site] ||= {}js_digests[:site_specific][site][bundle_key] ||= beginjs = bundle_js(bundle)- js.present? ? Digest::MD5.hexdigest(js) : nil+ js.present? ? digest_for_content(js) : nilendelsif bundle.in?(SHARED_BUNDLES)- js_digests[:shared][bundle_key] ||= Digest::MD5.hexdigest(bundle_js(bundle))+ js_digests[:shared][bundle_key] ||= digest_for_content(bundle_js(bundle))elseraise "Unknown bundle: #{bundle}"endenddef url(bundle)- "#{GlobalSetting.cdn_url}#{Discourse.base_path}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}&__ws=#{Discourse.current_hostname}"+ base = "#{GlobalSetting.cdn_url}#{Discourse.base_path}"+ path = "/extra-locales/#{bundle_js_hash(bundle)}/#{bundle}"+ query = SITE_SPECIFIC_BUNDLES.include?(bundle) ? "?__ws=#{Discourse.current_hostname}" : ""+ "#{base}#{path}#{query}"enddef client_overrides_exist?@@ -64,26 +67,30 @@ class ExtraLocalesController < ApplicationControllerdef bundle_js_with_hash(bundle)js = bundle_js(bundle)- [js, Digest::MD5.hexdigest(js)]+ [js, digest_for_content(js)]enddef clear_cache!site = RailsMultisite::ConnectionManagement.current_dbjs_digests[:site_specific].delete(site)end++ def digest_for_content(js)+ Digest::SHA1.hexdigest(js)+ endenddef showbundle = params[:bundle]raise Discourse::InvalidAccess.new if !valid_bundle?(bundle)- version = params[:v]- if version.present?- raise Discourse::InvalidParameters.new(:v) unless version.to_s.size == MD5_HASH_LENGTH+ digest = params[:digest]+ if digest.present?+ raise Discourse::InvalidParameters.new(:digest) unless digest.to_s.size == SHA1_HASH_LENGTHendcontent, hash = ExtraLocalesController.bundle_js_with_hash(bundle)- immutable_for(1.year) if hash == version+ immutable_for(1.year) if hash == digestrender plain: content, content_type: "application/javascript"enddiff --git a/config/routes.rb b/config/routes.rbindex 75e5ed6bfbb..a426697a5dd 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -460,7 +460,7 @@ Discourse::Application.routes.draw doget "email/unsubscribed" => "email#unsubscribed", :as => "email_unsubscribed"post "email/unsubscribe/:key" => "email#perform_unsubscribe", :as => "email_perform_unsubscribe"- get "extra-locales/:bundle" => "extra_locales#show"+ get "extra-locales/:digest/:bundle" => "extra_locales#show"resources :session, id: RouteFormat.username, only: %i[create destroy become] doget "become" if !Rails.env.production?diff --git a/spec/requests/extra_locales_controller_spec.rb b/spec/requests/extra_locales_controller_spec.rbindex 098aa59256a..4a531586130 100644--- a/spec/requests/extra_locales_controller_spec.rb+++ b/spec/requests/extra_locales_controller_spec.rb@@ -2,41 +2,39 @@RSpec.describe ExtraLocalesController doaround { |example| allow_missing_translations(&example) }+ let(:fake_digest) { "a" * 40 }describe "#show" doit "won't work with a weird parameter" do- get "/extra-locales/-invalid..character!!"+ get "/extra-locales/#{fake_digest}/-invalid..character!!"expect(response.status).to eq(404)endit "needs a valid bundle" do- get "/extra-locales/made-up-bundle"+ get "/extra-locales/#{fake_digest}/made-up-bundle"expect(response.status).to eq(403)end- it "requires a valid version" do- get "/extra-locales/overrides", params: { v: "a" }- expect(response.status).to eq(400)-- get "/extra-locales/overrides?v[foo]=1"+ it "requires a valid digest" do+ get "/extra-locales/ZZZ/overrides"expect(response.status).to eq(400)endit "caches for 1 year if version is provided and it matches current hash" do- get "/extra-locales/admin", params: { v: ExtraLocalesController.bundle_js_hash("admin") }+ get "/extra-locales/#{ExtraLocalesController.bundle_js_hash("admin")}/admin"expect(response.status).to eq(200)expect(response.headers["Cache-Control"]).to eq("max-age=31556952, public, immutable")endit "does not cache at all if version is invalid" do- get "/extra-locales/admin", params: { v: "a" * 32 }+ get "/extra-locales/#{fake_digest}/admin"expect(response.status).to eq(200)expect(response.headers["Cache-Control"]).not_to include("max-age", "public", "immutable")endit "doesn’t generate the bundle twice" dodescribed_class.expects(:bundle_js).returns("JS").once- get "/extra-locales/admin", params: { v: "a" * 32 }+ get "/extra-locales/#{fake_digest}/admin"endcontext "with plugin" do@@ -62,7 +60,7 @@ RSpec.describe ExtraLocalesController doafter { JsLocaleHelper.clear_cache! }it "includes plugin translations" do- get "/extra-locales/admin"+ get "/extra-locales/#{fake_digest}/admin"expect(response.status).to eq(200)expect(response.body.include?("github_badges")).to eq(true)end@@ -74,16 +72,13 @@ RSpec.describe ExtraLocalesController doit "works for anonymous users" doTranslationOverride.upsert!(I18n.locale, "js.some_key", "client-side translation")- get "/extra-locales/overrides",- params: {- v: ExtraLocalesController.bundle_js_hash("overrides"),- }+ get "/extra-locales/#{ExtraLocalesController.bundle_js_hash("overrides")}/overrides"expect(response.status).to eq(200)expect(response.headers["Cache-Control"]).to eq("max-age=31556952, public, immutable")endit "returns nothing when there are not overridden translations" do- get "/extra-locales/overrides"+ get "/extra-locales/#{fake_digest}/overrides"expect(response.status).to eq(200)expect(response.body).to be_emptyend@@ -104,7 +99,7 @@ RSpec.describe ExtraLocalesController do"{NUM_RESULTS, plural, one {1 result} other {many} }",)- get "/extra-locales/overrides"+ get "/extra-locales/#{fake_digest}/overrides"expect(response.status).to eq(200)expect(response.body).to_not include("server.some_key", "server.some_MF")@@ -153,7 +148,7 @@ RSpec.describe ExtraLocalesController douser = Fabricate(:user, locale: :fr)sign_in(user)- get "/extra-locales/overrides"+ get "/extra-locales/#{fake_digest}/overrides"expect(response.status).to eq(200)ctx = MiniRacer::Context.new@@ -174,7 +169,7 @@ RSpec.describe ExtraLocalesController dobefore { JsLocaleHelper.stubs(:output_MF).with("en").returns("MF_TRANSLATIONS") }it "returns the translations properly" do- get "/extra-locales/mf"+ get "/extra-locales/#{fake_digest}/mf"expect(response.body).to eq("MF_TRANSLATIONS")endend@@ -184,14 +179,14 @@ RSpec.describe ExtraLocalesController doit "doesn't call bundle_js more than once for the same locale and bundle" doI18n.locale = :deExtraLocalesController.expects(:bundle_js).with("admin").returns("admin_js DE").once- expected_hash_de = Digest::MD5.hexdigest("admin_js DE")+ expected_hash_de = Digest::SHA1.hexdigest("admin_js DE")expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_de)expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_de)I18n.locale = :frExtraLocalesController.expects(:bundle_js).with("admin").returns("admin_js FR").once- expected_hash_fr = Digest::MD5.hexdigest("admin_js FR")+ expected_hash_fr = Digest::SHA1.hexdigest("admin_js FR")expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_fr)expect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_fr)@@ -200,7 +195,7 @@ RSpec.describe ExtraLocalesController doexpect(ExtraLocalesController.bundle_js_hash("admin")).to eq(expected_hash_de)ExtraLocalesController.expects(:bundle_js).with("wizard").returns("wizard_js DE").once- expected_hash_de = Digest::MD5.hexdigest("wizard_js DE")+ expected_hash_de = Digest::SHA1.hexdigest("wizard_js DE")expect(ExtraLocalesController.bundle_js_hash("wizard")).to eq(expected_hash_de)expect(ExtraLocalesController.bundle_js_hash("wizard")).to eq(expected_hash_de)@@ -233,25 +228,32 @@ RSpec.describe ExtraLocalesController doit "returns both JS and its hash for a given bundle" doexpect(described_class.bundle_js_with_hash("admin")).to eq(- ["JS", Digest::MD5.hexdigest("JS")],+ ["JS", Digest::SHA1.hexdigest("JS")],)endenddescribe ".url" doit "works" do- expect(ExtraLocalesController.url("admin")).to start_with("/extra-locales/admin?v=")+ expect(ExtraLocalesController.url("admin")).to match(%r{\A/extra-locales/\h{40}\/admin\z})endit "includes subfolder path" doset_subfolder "/forum"- expect(ExtraLocalesController.url("admin")).to start_with("/forum/extra-locales/admin?v=")+ expect(ExtraLocalesController.url("admin")).to start_with("/forum/extra-locales/")endit "includes CDN" doset_cdn_url "https://cdn.example.com"expect(ExtraLocalesController.url("admin")).to start_with(- "https://cdn.example.com/extra-locales/admin?v=",+ "https://cdn.example.com/extra-locales/",+ )+ end++ it "includes hostname param for site-specific bundles" do+ set_cdn_url "https://cdn.example.com"+ expect(ExtraLocalesController.url("admin")).to start_with(+ "https://cdn.example.com/extra-locales/",)endend