Skip to content

Commit

Permalink
fix: respect host and protocol options in vite_preload_tag (#413)
Browse files Browse the repository at this point in the history
* Respect host and protocol options in vite_preload_tag

When specifying the host and protocol options for a vite_javascript_tag we want the preload tags to also respect those options.

* chore: use `extract!` instead

---------

Co-authored-by: Maximo Mussini <[email protected]>
  • Loading branch information
rywall and ElMassimo authored Nov 5, 2024
1 parent 57a6e7a commit bcd8151
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def test_vite_stylesheet_tag
}
end

def test_vite_preload_tag
assert_includes vite_typescript_tag('main', host: 'example.com', protocol: 'https'), [
%(<link rel="modulepreload" href="https://example.com/vite-production/assets/log.818edfb8.js" as="script" crossorigin="anonymous">),
%(<link rel="modulepreload" href="https://example.com/vite-production/assets/vue.3002ada6.js" as="script" crossorigin="anonymous">),
%(<link rel="modulepreload" href="https://example.com/vite-production/assets/vendor.0f7c0ec3.js" as="script" crossorigin="anonymous">),
].join("\n")
end

def test_vite_javascript_tag
assert_similar [
%(<script src="/vite-production/assets/main.9dcad042.js" crossorigin="anonymous" type="module"></script>),
Expand Down
3 changes: 2 additions & 1 deletion vite_rails/lib/vite_rails/tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def vite_manifest

# Internal: Renders a modulepreload link tag.
def vite_preload_tag(*sources, crossorigin:, **options)
asset_paths = sources.map { |source| path_to_asset(source) }
url_options = options.extract!(:host, :protocol)
asset_paths = sources.map { |source| path_to_asset(source, **url_options) }
try(:request).try(
:send_early_hints,
'Link' => asset_paths.map { |href|
Expand Down

0 comments on commit bcd8151

Please sign in to comment.