diff --git a/actionview/lib/action_view/helpers/asset_tag_helper.rb b/actionview/lib/action_view/helpers/asset_tag_helper.rb index 6a1d419a0a..b1efedccf6 100644 --- a/actionview/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionview/lib/action_view/helpers/asset_tag_helper.rb @@ -94,11 +94,12 @@ def javascript_include_tag(*sources) crossorigin = options.delete("crossorigin") crossorigin = "anonymous" if crossorigin == true integrity = options["integrity"] + rel = options["type"] == "module" ? "modulepreload" : "preload" sources_tags = sources.uniq.map { |source| href = path_to_javascript(source, path_options) if preload_links_header && !options["defer"] - preload_link = "<#{href}>; rel=preload; as=script" + preload_link = "<#{href}>; rel=#{rel}; as=script" preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil? preload_link += "; integrity=#{integrity}" unless integrity.nil? preload_link += "; nopush" if nopush diff --git a/actionview/test/template/asset_tag_helper_test.rb b/actionview/test/template/asset_tag_helper_test.rb index 56fdcb5c4b..a46074f3e6 100644 --- a/actionview/test/template/asset_tag_helper_test.rb +++ b/actionview/test/template/asset_tag_helper_test.rb @@ -554,6 +554,14 @@ def test_should_set_preload_links_with_cross_origin end end + def test_should_set_preload_links_with_rel_modulepreload + with_preload_links_header do + javascript_include_tag("http://example.com/all.js", type: "module") + expected = "; rel=modulepreload; as=script; nopush" + assert_equal expected, @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")