Skip to content

Commit

Permalink
Merge pull request #42338 from jhawthorn/send_preload_header_streaming
Browse files Browse the repository at this point in the history
Skip sending preload links headers when streaming
  • Loading branch information
jhawthorn committed Jun 1, 2021
2 parents cb8fc82 + f303eb2 commit 1658054
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions actionview/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -544,6 +544,7 @@ def resolve_link_as(extname, mime_type)
MAX_HEADER_SIZE = 8_000 # Some HTTP client and proxies have a 8kiB header limit
def send_preload_links_header(preload_links, max_header_size: MAX_HEADER_SIZE)
return if preload_links.empty?
return if response.sending?

if respond_to?(:request) && request
request.send_early_hints("Link" => preload_links.join("\n"))
Expand Down
50 changes: 40 additions & 10 deletions actionview/test/template/asset_tag_helper_test.rb
Expand Up @@ -6,9 +6,22 @@
require "action_dispatch"
ActionView::Template::Types.delegate_to Mime

module AssetTagHelperTestHelpers
def with_preload_links_header(new_preload_links_header = true)
original_preload_links_header = ActionView::Helpers::AssetTagHelper.preload_links_header
ActionView::Helpers::AssetTagHelper.preload_links_header = new_preload_links_header

yield
ensure
ActionView::Helpers::AssetTagHelper.preload_links_header = original_preload_links_header
end
end

class AssetTagHelperTest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper

include AssetTagHelperTestHelpers

attr_reader :request, :response

class FakeRequest
Expand All @@ -24,6 +37,7 @@ class FakeResponse
def headers
@headers ||= {}
end
def sending?; false; end
end

def setup
Expand Down Expand Up @@ -788,16 +802,6 @@ def test_caching_image_path_with_caching_and_proc_asset_host_using_request
assert_equal "http://localhost/images/xml.png", image_path("xml.png")
end
end

private
def with_preload_links_header(new_preload_links_header = true)
original_preload_links_header = ActionView::Helpers::AssetTagHelper.preload_links_header
ActionView::Helpers::AssetTagHelper.preload_links_header = new_preload_links_header

yield
ensure
ActionView::Helpers::AssetTagHelper.preload_links_header = original_preload_links_header
end
end

class AssetTagHelperNonVhostTest < ActionView::TestCase
Expand Down Expand Up @@ -951,6 +955,32 @@ def test_javascript_include_tag_without_request
end
end

class AssetTagHelperWithStreamingRequest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper

include AssetTagHelperTestHelpers

def setup
super
response.sending!
end

def test_stylesheet_link_tag_with_streaming
with_preload_links_header do
assert_dom_equal(
%(<link rel="stylesheet" href="/stylesheets/foo.css" />),
stylesheet_link_tag("foo.css")
)
end
end

def test_javascript_include_tag_with_streaming
with_preload_links_header do
assert_dom_equal %(<script src="/javascripts/foo.js"></script>), javascript_include_tag("foo.js")
end
end
end

class AssetUrlHelperControllerTest < ActionView::TestCase
tests ActionView::Helpers::AssetUrlHelper

Expand Down

0 comments on commit 1658054

Please sign in to comment.