FEATURE: Preload resources via link header (#18475)

Experiment moving from preload tags in the document head to preload information the the response headers.

While this is a minor improvement in most browsers (headers are parsed before the response body), this allows smart proxies like Cloudflare to "learn" from those headers and build HTTP 103 Early Hints for subsequent requests to the same URI, which will allow the user agent to download and parse our JS/CSS while we are waiting for the server to generate and stream the HTML response.

Co-authored-by: Penar Musaraj <pmusaraj@gmail.com>
This commit is contained in:
Rafael dos Santos Silva
2022-10-07 13:19:50 -03:00
committed by GitHub
parent a1d67122b1
commit 2d1dbc6f96
7 changed files with 107 additions and 17 deletions

View File

@ -148,6 +148,16 @@ RSpec.describe Stylesheet::Manager do
})
end
it "stylesheet_link_tag calls the preload callback when set" do
preload_list = []
preload_callback = ->(href, type) { preload_list << [href, type] }
manager = manager(theme.id)
expect {
manager.stylesheet_link_tag(:desktop_theme, 'all', preload_callback)
}.to change(preload_list, :size)
end
context "with stylesheet order" do
let(:z_child_theme) do
Fabricate(:theme, component: true, name: "ze component").tap do |z|
@ -638,6 +648,17 @@ RSpec.describe Stylesheet::Manager do
expect(details1[:new_href]).not_to eq(details2[:new_href])
end
it "calls the preload callback when set" do
preload_list = []
cs = Fabricate(:color_scheme, name: 'Funky')
theme = Fabricate(:theme, color_scheme_id: cs.id)
preload_callback = ->(href, type) { preload_list << [href, type] }
expect {
manager.color_scheme_stylesheet_link_tag(theme.id, 'all', preload_callback)
}.to change(preload_list, :size).by(1)
end
context "with theme colors" do
let(:theme) { Fabricate(:theme).tap { |t|
t.set_field(target: :common, name: "color_definitions", value: ':root {--special: rebeccapurple;}')