Skip to content

Commit bf36135

Browse files
authored
Document default of defer for javascript_pack_tag (#3142)
1 parent 5de0fbc commit bf36135

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ packs with the chunks in your view, which creates html tags for all the chunks.
138138
The result looks like this:
139139

140140
```erb
141-
<%= javascript_pack_tag 'calendar', 'map' %>
141+
<%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
142142
143-
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
144-
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
145-
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
146-
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
147-
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
143+
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
144+
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
145+
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload" defer"></script>
146+
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
147+
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
148148
```
149149

150150
**Important:** Pass all your pack names as multiple arguments, not multiple calls, when using `javascript_pack_tag` and the **`stylesheet_pack_tag`. Otherwise, you will get duplicated chunks on the page. Be especially careful if you might be calling these view helpers from your view, partials, and the layout for a page. You will need some logic to ensure you call the helpers only once with multiple arguments.
@@ -189,14 +189,15 @@ If you want to use images in your stylesheets:
189189
background-image: url('../images/logo.svg')
190190
}
191191
```
192+
##### Defer for `javascript_pack_tag`
193+
Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
192194

193195
#### Server-Side Rendering (SSR)
194196

195197
Note, if you are using server-side rendering of JavaScript with dynamic code-spliting, as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails), your JavaScript should create the link prefetch HTML tags that you will use, so you won't need to use to `asset_pack_path` in those circumstances.
196198

197199
**Note:** In order for your styles or static assets files to be available in your view, you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know to package up those files.
198200

199-
200201
### Development
201202

202203
Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`. Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js` executables to ensure that the right configuration files and environmental variables are loaded based on your environment.

0 commit comments

Comments
 (0)