PERF: Improve Accept header handling for stylesheets and theme-js (#19357)

The default behavior for Rails is to vary the response of an endpoint based on the `Accept:` header, and therefore it returns a `Vary:` header on responses. This instructs browsers and intermediate proxies to key their caches based on the value of the request's `Accept` header. In some cases (e.g. Akamai), the presence of a `Vary` header is enough to prevent caching entirely.

This commit restructures the Rails route definitions so that:
1. The "format" segment of the route is 'required'
2. The "format" segment of the route is constrained to a single value (e.g. `js` or `css`)

Now that the routes are guaranteed to have a `:format` segment, Rails will always prioritize that over the `Accept` header, and will therefore omit the `Vary` header.

Request specs are also added to test this behaviour for both stylesheets and theme-javascripts.
This commit is contained in:
David Taylor
2022-12-07 15:46:35 +00:00
committed by GitHub
parent fc22790405
commit 1db3a578e4
3 changed files with 47 additions and 4 deletions
@@ -75,6 +75,25 @@ RSpec.describe ThemeJavascriptsController do
//# sourceMappingURL=#{digest}.map?__ws=test.localhost
JS
end
it "ignores Accept header and does not return Vary header" do
js_cache_url = "/theme-javascripts/#{javascript_cache.digest}.js"
get js_cache_url
expect(response.status).to eq(200)
expect(response.headers["Content-Type"]).to eq("text/javascript")
expect(response.headers["Vary"]).to eq(nil)
get js_cache_url, headers: { "Accept" => "text/html" }
expect(response.status).to eq(200)
expect(response.headers["Content-Type"]).to eq("text/javascript")
expect(response.headers["Vary"]).to eq(nil)
get js_cache_url, headers: { "Accept" => "invalidcontenttype" }
expect(response.status).to eq(200)
expect(response.headers["Content-Type"]).to eq("text/javascript")
expect(response.headers["Vary"]).to eq(nil)
end
end
describe "#show_map" do