DEV: Consolidate experimental 'Link' header implementations (#26377)

This commit removes the 'experimental_preconnect_link_header' site setting, and the 'preload_link_header' site setting, and introduces two new global settings: early_hint_header_mode and early_hint_header_name.

We don't actually send 103 Early Hint responses from Discourse. However, upstream proxies can be configured to cache a response header from the app and use that to send an Early Hint response to future clients.

- `early_hint_header_mode` specifies the mode for the early hint header. Can be nil (disabled), "preconnect" (lists just CDN domains) or "preload" (lists all assets).
- `early_hint_header_name` specifies which header name to use for the early hint. Defaults to "Link", but can be changed to support different proxy mechanisms.
This commit is contained in:
David Taylor
2024-03-27 09:06:50 +00:00
committed by GitHub
parent 23fc0fb078
commit 1cc8c72a98
7 changed files with 68 additions and 79 deletions

View File

@ -1188,60 +1188,50 @@ RSpec.describe ApplicationController do
end
end
describe "Link header" do
describe "when `experimental_preconnect_link_header` site setting is enabled" do
before { SiteSetting.experimental_preconnect_link_header = true }
describe "Early hint header" do
before { global_setting :cdn_url, "https://cdn.example.com/something" }
it "should include the `preconnect` and `dns-prefetch` resource hints in the Link header when `GlobalSetting.cdn_url is configured`" do
global_setting :cdn_url, "https://cdn.example.com/something"
it "is not included by default" do
get "/latest"
expect(response.status).to eq(200)
expect(response.headers["Link"]).to eq(nil)
end
context "when in preconnect mode" do
before { global_setting :early_hint_header_mode, "preconnect" }
it "includes the preconnect hint" do
get "/latest"
expect(response.status).to eq(200)
expect(response.headers["Link"]).to include(
"<https://cdn.example.com>; rel=preconnect, <https://cdn.example.com>; rel=dns-prefetch",
)
expect(response.headers["Link"]).to include("<https://cdn.example.com>; rel=preconnect")
expect(response.headers["Link"]).not_to include("rel=preload")
end
it "should include the `preconnect` and `dns-prefetch` resource hints in the Link header when `SiteSetting.s3_cdn_url is configured`" do
SiteSetting.s3_cdn_url = "https://s3.some-cdn.com/something"
it "can use a different header" do
global_setting :early_hint_header_name, "X-Discourse-Early-Hint"
get "/latest"
expect(response.status).to eq(200)
expect(response.headers["Link"]).to include(
"<https://s3.some-cdn.com>; rel=preconnect, <https://s3.some-cdn.com>; rel=dns-prefetch",
expect(response.headers["X-Discourse-Early-Hint"]).to include(
"<https://cdn.example.com>; rel=preconnect",
)
expect(response.headers["Link"]).to eq(nil)
end
it "is skipped for non-app URLs" do
get "/latest.json"
expect(response.status).to eq(200)
expect(response.headers["Link"]).to eq(nil)
end
end
context "when `GlobalSetting.preload_link_header` is enabled" do
before { global_setting :preload_link_header, true }
context "when in preload mode" do
before { global_setting :early_hint_header_mode, "preload" }
it "should have the Link header with assets on full page requests" do
get("/latest")
expect(response.headers).to include("Link")
end
it "shouldn't have the Link header on xhr api requests" do
get("/latest.json")
expect(response.headers).not_to include("Link")
end
end
context "when `GlobalSetting.preload_link_header` is disabled" do
before { global_setting :preload_link_header, false }
it "shouldn't have the Link header with assets on full page requests" do
get("/latest")
expect(response.headers).not_to include("Link")
end
it "shouldn't have the Link header on xhr api requests" do
get("/latest.json")
expect(response.headers).not_to include("Link")
it "includes the preload hint" do
get "/latest"
expect(response.status).to eq(200)
expect(response.headers["Link"]).to include('.js>; rel="preload"')
expect(response.headers["Link"]).to include('.css?__ws=test.localhost>; rel="preload"')
end
end
end