Skip to content

Commit

Permalink
Make it possible to opt-out of sending Link header in preload_link_tag
Browse files Browse the repository at this point in the history
This commit adds a `preload_links_header` option to `preload_link_tag`
to disable sending of the Link header in the HTTP response.

Currently `ActionView::Helpers::AssetTagHelper.preload_links_header`
only controls whether `javascript_include_tag` and
`stylesheet_link_tag` send the Link header, but there is no way to
control the behavior of `preload_link_tag`. We could just check that
variable, but existing applications might be relying on
`preload_link_tag` to add the header. Users also might want control
over this on a per call basis.

Closes #51436
  • Loading branch information
stanhu committed Mar 28, 2024
1 parent 8e46af8 commit 6578fbf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
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
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

0 comments on commit 6578fbf

Please sign in to comment.