Skip to content

Commit a003325

Browse files
guillaumebridaygauravtiwari
authored andcommitted
Add a preload_pack_asset helper (#2124)
1 parent b4057d7 commit a003325

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

docs/webpack.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ environment.splitChunks()
277277
environment.splitChunks((config) => Object.assign({}, config, { optimization: { splitChunks: false }}))
278278
```
279279

280-
Then use, `javascript_packs_with_chunks_tag` helper to include all the transpiled
280+
Then use the `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` helpers to include all the transpiled
281281
packs with the chunks in your view, which creates html tags for all the chunks.
282282

283283
```erb
@@ -304,6 +304,20 @@ get duplicated chunks on the page.
304304

305305
For the old configuration with the CommonsChunkPlugin see below. **Note** that this functionality is deprecated in Webpack V4.
306306

307+
#### Preloading
308+
309+
Before preload or prefetch your assets, please read [https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content](https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content).
310+
311+
Webpack also provide it's own methods for preload or prefetch [https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c](https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c).
312+
313+
You can preload your assets with the `preload_pack_asset` helper if you have Rails >= 5.2.x.
314+
315+
```erb
316+
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
317+
```
318+
319+
**Warning:** You don't want to preload the css, you want to preload the fonts and images inside the css so that fonts, css, and images can all be downloaded in parallel instead of waiting for the browser to parse the css.
320+
307321
### Add common chunks (deprecated in Webpack V4)
308322

309323
The CommonsChunkPlugin is an opt-in feature that creates a separate file (known as a chunk), consisting of common modules shared between multiple entry points. By separating common modules from bundles, the resulting chunked file can be loaded once initially, and stored in the cache for later use. This results in page speed optimizations as the browser can quickly serve the shared code from the cache, rather than being forced to load a larger bundle whenever a new page is visited.

lib/webpacker/helper.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,27 @@ def javascript_pack_tag(*names, **options)
7878
# DO:
7979
# <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
8080
# DON'T:
81-
# <%= javascript_packs_with_chunks_tag 'calendar' %>
81+
# <%= javascript_packs_with_chunks_tag 'calendar' %>
8282
# <%= javascript_packs_with_chunks_tag 'map' %>
8383
def javascript_packs_with_chunks_tag(*names, **options)
8484
javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
8585
end
8686

87+
# Creates a link tag, for preloading, that references a given Webpacker asset
88+
# In production mode, the digested reference is automatically looked up.
89+
# See: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
90+
# Example:
91+
#
92+
# <%= preload_pack_asset 'fonts/fa-regular-400.woff2' %> # =>
93+
# <link rel="preload" href="/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2" as="font" type="font/woff2" crossorigin="anonymous">
94+
def preload_pack_asset(name, **options)
95+
if self.class.method_defined?(:preload_link_tag)
96+
preload_link_tag(current_webpacker_instance.manifest.lookup!(name), options)
97+
else
98+
raise "You need Rails >= 5.2 to use this tag."
99+
end
100+
end
101+
87102
# Creates a link tag that references the named pack file, as compiled by webpack per the entries list
88103
# in config/webpack/shared.js. By default, this list is auto-generated to match everything in
89104
# app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
@@ -120,7 +135,7 @@ def stylesheet_pack_tag(*names, **options)
120135
# DO:
121136
# <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %>
122137
# DON'T:
123-
# <%= stylesheet_packs_with_chunks_tag 'calendar' %>
138+
# <%= stylesheet_packs_with_chunks_tag 'calendar' %>
124139
# <%= stylesheet_packs_with_chunks_tag 'map' %>
125140
def stylesheet_packs_with_chunks_tag(*names, **options)
126141
if current_webpacker_instance.config.extract_css?

test/helper_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ def test_javascript_pack_tag_split_chunks
7979
javascript_packs_with_chunks_tag("application")
8080
end
8181

82+
def test_preload_pack_asset
83+
if self.class.method_defined?(:preload_link_tag)
84+
assert_equal \
85+
%(<link rel="preload" href="/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2" as="font" type="font/woff2" crossorigin="anonymous">),
86+
preload_pack_asset("fonts/fa-regular-400.woff2")
87+
else
88+
error = assert_raises do
89+
preload_pack_asset("fonts/fa-regular-400.woff2")
90+
end
91+
92+
assert_equal \
93+
"You need Rails >= 5.2 to use this tag.",
94+
error.message
95+
end
96+
end
97+
8298
def test_stylesheet_pack_tag_split_chunks
8399
assert_equal \
84100
%(<link rel="stylesheet" media="screen" href="/packs/1-c20632e7baf2c81200d3.chunk.css" />\n) +

test/test_app/public/packs/manifest.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"bootstrap.js": "/packs/bootstrap-300631c4f0e0f9c865bc.js",
55
"application.js": "/packs/application-k344a6d59eef8632c9d1.js",
66
"application.png": "/packs/application-k344a6d59eef8632c9d1.png",
7+
"fonts/fa-regular-400.woff2": "/packs/fonts/fa-regular-400-944fb546bd7018b07190a32244f67dc9.woff2",
78
"media/images/image.jpg": "/packs/media/images/image-c38deda30895059837cf.jpg",
89
"media/images/nested/image.jpg": "/packs/media/images/nested/image-c38deda30895059837cf.jpg",
910
"entrypoints": {

0 commit comments

Comments
 (0)