Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to opt-out of sending Link header in preload_link_tag #51441

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,7 @@
* Add the `preload_links_header` option for `preload_link_tag` to opt-out of sending the Link header.

*Stan Hu*

* Raise `ArgumentError` if `:renderable` object does not respond to `#render_in`

*Sean Doyle*
Expand Down
23 changes: 12 additions & 11 deletions actionview/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -357,25 +357,26 @@ def preload_link_tag(source, options = {})
crossorigin = "anonymous" if crossorigin == true || (crossorigin.blank? && as_type == "font")
integrity = options[:integrity]
nopush = options.delete(:nopush) || false
send_link_header = options.key?(:preload_links_header) ? options.delete(:preload_links_header) : true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think early_hint: false would be a better name for this.

rel = mime_type == "module" ? "modulepreload" : "preload"

link_tag = tag.link(
if send_link_header
preload_link = "<#{href}>; rel=#{rel}; as=#{as_type}"
preload_link += "; type=#{mime_type}" if mime_type
preload_link += "; crossorigin=#{crossorigin}" if crossorigin
preload_link += "; integrity=#{integrity}" if integrity
preload_link += "; nopush" if nopush

send_preload_links_header([preload_link])
end

tag.link(
rel: rel,
href: href,
as: as_type,
type: mime_type,
crossorigin: crossorigin,
**options.symbolize_keys)

preload_link = "<#{href}>; rel=#{rel}; as=#{as_type}"
preload_link += "; type=#{mime_type}" if mime_type
preload_link += "; crossorigin=#{crossorigin}" if crossorigin
preload_link += "; integrity=#{integrity}" if integrity
preload_link += "; nopush" if nopush

send_preload_links_header([preload_link])

link_tag
end

# Returns an HTML image tag for the +source+. The +source+ can be a full
Expand Down
7 changes: 7 additions & 0 deletions actionview/test/template/asset_tag_helper_test.rb
Expand Up @@ -715,6 +715,13 @@ def test_should_set_preload_early_hints_with_rel_modulepreload
end
end

def test_should_not_set_preload_link_with_rel_modulepreload
with_preload_links_header do
preload_link_tag("http://example.com/all.js", type: "module", preload_links_header: false)
assert_nil @response.headers["Link"]
end
end

def test_should_set_preload_links_with_integrity_hashes
with_preload_links_header do
stylesheet_link_tag("http://example.com/style.css", integrity: "sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs")
Expand Down