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
add support for link preload/prefetch #7056
Conversation
`import(/* webpackPrefetchPriority: 10 */ "...")` `import(/* webpackPreloadPriority: 10 */ "...")`
Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon. |
A question here. Would it make sense to add aliases for common use cases of preloads? I’m thinking of something like import(/* webpackPreloadPriority: "preload" */ './foo.js')
import(/* webpackPrefetchPriority: "prefetch" */ './foo.js')
import(/* webpackPrefetchPriority: "prefetch-unlikely" */ './foo.js') in addition to import(/* webpackPreloadPriority: 1 */ './foo.js')
import(/* webpackPrefetchPriority: 1 */ './foo.js')
import(/* webpackPrefetchPriority: 5 */ './foo.js') The reason I’m thinking about this is
|
Oh, wait, I’m dumb, that’s not a priority, that’s an order |
Well, OK. Would it make sense to rename Continuing on
the following might do a great job in making the feature simpler (& more popular!): import(/* webpackPreload: true */ 'foo.js') // Works as webpackPreloadOrder: 1
import(/* webpackPrefetch: true */ 'foo.js') // Works as webpackPrefetchOrder: 1 |
rename webpackPreloadPriority to webpackPreload allow true as parameter (equals 0)
Thanks for the feedback @iamakulov I changed it to |
The minimum test ratio has been reached. Thanks! |
|
Following up on @sokra's work in webpack#7056, this change addresses webpack#7084 to have webpack prefetch and preload designated chunks from the entry chunk.
What kind of change does this PR introduce?
feature
Did you add tests for your changes?
yes
If relevant, link to documentation update:
TODO
Summary
This adds support for a
webpackPreload
resp.webpackPrefetch
magic comment atimport()
which automatically adds<link rel="preload/prefetch">
tags after the parent chunk is loaded. This prefetches the scripts when the browser is idle and improves latency resp. preloads a script that is expected to be used imediately. When multipleimport()
s are accessible they are prefetch/preloading by importance (highest first, can be provided as number argument).Prefetching is similar to precaching the whole application in a service worker, but this PR only prefetches the next possible on-demand chunks and in the order you specify. This saves bandwidth and more likely accessed chunks can be prefetched first.
Preload allows the browser to prepare the script in background which also improves performance, and elimitates waterfall-like
import()
. Use preload wisely and only when you know what you are doing.Preloading/prefetching is also available on entry points but this requires support by the plugin you are using to generate the HTML. webpack now provides preload/prefetch info in the json stats object in
entrypoints.childAssets
. This can be used to generate<link>
tags in the HTML (Note that the normal assets for the entrypoint also need to be added as<link>
tags to ensure they load first, when using preload).Does this PR introduce a breaking change?
I hope not, but there are a few API changes which should be backward-compatible
Other information
see also https://github.com/GoogleChromeLabs/preload-webpack-plugin. Compared to this plugin this PR prefetches only the next layer in the application and allows to specify the order. cc @addyosmani
As guideline:
webpackPrefetch: true
toimport()
s that the user will likely visit in future.webpackPreload: true
toimport()
s that your code will always load immediately.webpackPrefetch: -10
toimport()
s that the user will probably visit in future and low latency is very important.