Skip to content

Add webpack_assets_path_tag to apply application asset host to dynamic/lazy chunk source path #2636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Now, you can set `brotli_static on;` in your nginx site config, as per the confi
Webpacker out-of-the-box provides CDN support using your Rails app `config.action_controller.asset_host` setting. If you already have [CDN](http://guides.rubyonrails.org/asset_pipeline.html#cdns) added in your Rails app
you don't need to do anything extra for Webpacker, it just works.

Note: If you rely on dynamic/lazy chunk loading, you should either compile packs with static `WEBPACKER_ASSET_HOST` or use `webpack_assets_path_tag` to override chunk loading source in runtime.

## Capistrano

### Assets compiling on every deployment even if JavaScript and CSS files are not changed
Expand Down
17 changes: 17 additions & 0 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ def stylesheet_packs_with_chunks_tag(*names, **options)
end
end

# Overrides Webpack public path in runtime to apply application asset host.
# That forces Webpack to take into account application asset host for dynamic/lazy chunk loading without a necessity
# to pre-build bundles with static `WEBPACKER_ASSET_HOST`.
#
# Examples:
#
# # Should be used in `head` before any `*_pack*` helpers
# <%= webpack_assets_path_tag %>
#
# <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %>
# <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %>
def webpack_assets_path_tag(html_options = {})
path = "#{compute_asset_host}/#{current_webpacker_instance.config.public_output_path.basename}/"

javascript_tag("__webpack_public_path__ = '#{path}';", html_options)
end

private
def stylesheet?(name)
File.extname(name) == ".css"
Expand Down
35 changes: 35 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def send_early_hints(links) end
def base_url
"https://example.com"
end
def protocol
"https://"
end
end.new
end

Expand Down Expand Up @@ -142,4 +145,36 @@ def test_stylesheet_pack_tag_splat
%(<link rel="stylesheet" media="all" href="/packs/application-dd6b1cd38bfa093df600.css" />),
stylesheet_pack_tag("bootstrap.css", "application.css", media: "all")
end

def test_webpack_assets_path_tag
assert_equal \
%(<script>\n) +
%(//<![CDATA[\n) +
%(__webpack_public_path__ = '/packs/';\n) +
%(//]]>\n) +
%(</script>),
webpack_assets_path_tag
end

def test_webpack_assets_path_tag_with_asset_host
config.stub :asset_host, "cdn.example.com" do
assert_equal \
%(<script>\n) +
%(//<![CDATA[\n) +
%(__webpack_public_path__ = 'https://cdn.example.com/packs/';\n) +
%(//]]>\n) +
%(</script>),
webpack_assets_path_tag
end
end

def test_webpack_assets_path_tag_with_html_options
assert_equal \
%(<script defer="defer">\n) +
%(//<![CDATA[\n) +
%(__webpack_public_path__ = '/packs/';\n) +
%(//]]>\n) +
%(</script>),
webpack_assets_path_tag(defer: "defer")
end
end