1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #40092 from fleck/dont-push-assets-and-skip-preload-for-defer-javascript

don't add preload headers for deferred scripts and add nopush
This commit is contained in:
Rafael França 2020-08-24 14:53:51 -04:00 committed by GitHub
commit acaa546026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -87,10 +87,15 @@ module ActionView
options = sources.extract_options!.stringify_keys
path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
preload_links = []
nopush = options["nopush"].nil? ? true : options.delete("nopush")
sources_tags = sources.uniq.map { |source|
href = path_to_javascript(source, path_options)
preload_links << "<#{href}>; rel=preload; as=script"
unless options["defer"]
preload_link = "<#{href}>; rel=preload; as=script"
preload_link += "; nopush" if nopush
preload_links << preload_link
end
tag_options = {
"src" => href
}.merge!(options)
@ -137,10 +142,13 @@ module ActionView
options = sources.extract_options!.stringify_keys
path_options = options.extract!("protocol", "host", "skip_pipeline").symbolize_keys
preload_links = []
nopush = options["nopush"].nil? ? true : options.delete("nopush")
sources_tags = sources.uniq.map { |source|
href = path_to_stylesheet(source, path_options)
preload_links << "<#{href}>; rel=preload; as=style"
preload_link = "<#{href}>; rel=preload; as=style"
preload_link += "; nopush" if nopush
preload_links << preload_link
tag_options = {
"rel" => "stylesheet",
"media" => "screen",

View file

@ -513,6 +513,18 @@ class AssetTagHelperTest < ActionView::TestCase
def test_should_set_preload_links
stylesheet_link_tag("http://example.com/style.css")
javascript_include_tag("http://example.com/all.js")
expected = "<http://example.com/style.css>; rel=preload; as=style; nopush,<http://example.com/all.js>; rel=preload; as=script; nopush"
assert_equal expected, @response.headers["Link"]
end
def test_should_not_preload_links_with_defer
javascript_include_tag("http://example.com/all.js", defer: true)
assert_equal "", @response.headers["Link"]
end
def test_should_allow_caller_to_remove_nopush
stylesheet_link_tag("http://example.com/style.css", nopush: false)
javascript_include_tag("http://example.com/all.js", nopush: false)
expected = "<http://example.com/style.css>; rel=preload; as=style,<http://example.com/all.js>; rel=preload; as=script"
assert_equal expected, @response.headers["Link"]
end