mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 09:22:42 +08:00
EXPERIMENTAL: preconnect and dns-prefetch resource hints for CDN domains (#26215)
Why this change? In https://web.dev/articles/preconnect-and-dns-prefetch, it describes how hinting to the browser to preconnect to domains which we will eventually use the connection for can help improve the time it takes to load a page. We are putting this behind an experimental flag so that we can test and profile this in a production environment. What does this change introduce? Introduce a hidden experimental `experimental_preconnect_link_header` site setting which when enabled will add the `preconnect` and `dns-prefetch` resource hints to the response headers for full page load requests.
This commit is contained in:

committed by
GitHub

parent
d5b944f1de
commit
36cdb1444c
@ -53,7 +53,7 @@ class ApplicationController < ActionController::Base
|
||||
after_action :add_noindex_header_to_non_canonical, if: :spa_boot_request?
|
||||
after_action :set_cross_origin_opener_policy_header, if: :spa_boot_request?
|
||||
after_action :clean_xml, if: :is_feed_response?
|
||||
around_action :link_preload, if: -> { spa_boot_request? && GlobalSetting.preload_link_header }
|
||||
around_action :add_link_header, if: -> { spa_boot_request? }
|
||||
|
||||
HONEYPOT_KEY ||= "HONEYPOT_KEY"
|
||||
CHALLENGE_KEY ||= "CHALLENGE_KEY"
|
||||
@ -1096,10 +1096,29 @@ class ApplicationController < ActionController::Base
|
||||
result
|
||||
end
|
||||
|
||||
def link_preload
|
||||
@links_to_preload = []
|
||||
def add_link_header
|
||||
@links_to_preload = [] if GlobalSetting.preload_link_header
|
||||
|
||||
yield
|
||||
response.headers["Link"] = @links_to_preload.join(", ") if !@links_to_preload.empty?
|
||||
|
||||
links = []
|
||||
|
||||
if SiteSetting.experimental_preconnect_link_header
|
||||
[GlobalSetting.cdn_url, SiteSetting.s3_cdn_url].each do |url|
|
||||
next if url.blank?
|
||||
base_url = URI.join(url, "/").to_s.chomp("/")
|
||||
|
||||
links.push("<#{base_url}>; rel=preconnect;")
|
||||
# Not all browsers support the preconnect resource hint so we are adding dns-prefetch as the fallback
|
||||
links.push("<#{base_url}>; rel=dns-prefetch;")
|
||||
end
|
||||
end
|
||||
|
||||
if GlobalSetting.preload_link_header && !@links_to_preload.empty?
|
||||
links = links.concat(@links_to_preload)
|
||||
end
|
||||
|
||||
response.headers["Link"] = links.join(", ") if links.present?
|
||||
end
|
||||
|
||||
def spa_boot_request?
|
||||
|
Reference in New Issue
Block a user