feat(gatsby): enable granular chunks#22253
Conversation
There was a problem hiding this comment.
this regex probably needs directory boundaries to avoid /react/ matching react-foo.
1635e9c to
d918a05
Compare
|
Yes, I'd say https://www.gatsbyjs.org/docs/production-app/ should be updated as part of this. |
Improved webpack bundling by optimizing caching and less overall javascript
d918a05 to
946f6d3
Compare
laurieontech
left a comment
There was a problem hiding this comment.
Thanks Ward! Left some suggestions below but looks really good.
| commons: {}, | ||
| // When a module is used more than once we create a shared bundle to save user's bandwidth | ||
| shared: {}, | ||
| // All css is bundled into one stylesheet |
There was a problem hiding this comment.
| // All css is bundled into one stylesheet | |
| // All CSS is bundled into one stylesheet |
| }, | ||
| // Keep maximum initial requests to 25 | ||
| maxInitialRequests: 25, | ||
| // A chunk should be at least 20kb to be considered into splitChunks |
There was a problem hiding this comment.
| // A chunk should be at least 20kb to be considered into splitChunks | |
| // A chunk should be at least 20kb before using splitChunks |
| minimizers: [ | ||
| // Minify javascript using Terser (https://terser.org/) | ||
| plugins.minifyJs(), | ||
| // Minify css by using cssnano (https://cssnano.co/) |
There was a problem hiding this comment.
| // Minify css by using cssnano (https://cssnano.co/) | |
| // Minify CSS by using cssnano (https://cssnano.co/) |
|
|
||
| ##### framework-[contenthash].js | ||
|
|
||
| The framework bundle contains the react framework. We've noticed that React hardly gets upgraded to a newer version. Creating a separate bundle improves users' browser cache hit rate as this bundle is likely not going to be updated often. |
There was a problem hiding this comment.
| The framework bundle contains the react framework. We've noticed that React hardly gets upgraded to a newer version. Creating a separate bundle improves users' browser cache hit rate as this bundle is likely not going to be updated often. | |
| The framework bundle contains the React framework. Based on user behavior, React hardly gets upgraded to a newer version. Creating a separate bundle improves users' browser cache hit rate as this bundle is likely not going to be updated often. |
|
|
||
| ##### commons-[contenthash].js | ||
|
|
||
| Libraries used on every gatsby page are bundled into the commons javascript file. By bundling these all together, we make sure your user only needs to download this bundle once. |
There was a problem hiding this comment.
| Libraries used on every gatsby page are bundled into the commons javascript file. By bundling these all together, we make sure your user only needs to download this bundle once. | |
| Libraries used on every Gatsby page are bundled into the commons javascript file. By bundling these together, you can make sure your users only need to download this bundle once. |
laurieontech
left a comment
There was a problem hiding this comment.
Thanks Ward! Would it be possible to add a paragraph that just primes the user on chunking as a concept?
To the app-production.md file? Not 100% sure what you want me to explain, chunks & modules are webpack terms. Technically a module contains chunks & chunks are pieces of code (a file or bundle of files) |
|
This section has an intro paragraph. Perhaps mention that we make use of chunking with a link to the applicable webpack docs on the topic? |
2cf2fab to
2b26f06
Compare
laurieontech
left a comment
There was a problem hiding this comment.
Thanks Ward! Think that helps a lot. I made some suggestions since we're trying to move away from "we", but overall think it looks good.
Co-Authored-By: LB <laurie@gatsbyjs.com>
laurieontech
left a comment
There was a problem hiding this comment.
Thanks for adding the right docs info!
LekoArts
left a comment
There was a problem hiding this comment.
Added just one question :)
KyleAMathews
left a comment
There was a problem hiding this comment.
Looks awesome! Excited to get this in!
|
Published in |
|
(very late to the party, but 💜 🙏 🎉) |
Enable granular chunking for Gatsby. This PR helps us improve our webpages in 2 ways: Less duplications (less javascript) By bundling a dependency that is at least used in 2 pages we can bundle them together so we don't have to download duplicate libraries over and over again. This won't benefit first-page load but it improves page navigation as you'll need less javascript for the next route. Big libraries over 160kb are moved to a separate library all together to improve js parsing & execution costs. Our commons chunk is used to put in code that is used over all our pages (commons). So we don't bloat any pages more than we need Caching We've added a framework bundle which contains react, react-dom as this is a chunk that will hardly ever change. Commons & shared libraries can change when new pages are created which is more likely to happen than react being upgraded. We might want to move other modules into it, like @reach/router. Overall caching is improved as chunks will change less often.
Description
Enable granular chunking for Gatsby. This PR helps us improve our webpages in 2 ways:
Less duplications (less javascript)
By bundling a dependency that is at least used in 2 pages we can bundle them together so we don't have to download duplicate libraries over and over again. This won't benefit first-page load but it improves page navigation as you'll need less javascript for the next route.
Big libraries over 160kb are moved to a separate library all together to improve js parsing & execution costs.
Our commons chunk is used to put in code that is used over all our pages (commons). So we don't bloat any pages more than we need
Caching
We've added a framework bundle which contains react, react-dom as this is a chunk that will hardly ever change. Commons & shared libraries can change when new pages are created which is more likely to happen than react being upgraded. We might want to move other modules into it, like @reach/router.
Overall caching is improved as chunks will change less often.
These are the links for gatsbyjs.org & reactjs.org as they are opensource sites.
gatsbyjs.org before
gatsbyjs.org after
reactjs.org before
reactjs.org after
What's next?
MDX is still a black box as it's now added to app.js and should be part of the shared libs, which is not the case. Firstly I'll be looking at our polyfill strategy to reduce our bundles and afterwards, I'll check out how to optimize mdx.
Big shoutout to @developit, @housseindjirdeh and the nextjs team
Documentation
I might need to update https://www.gatsbyjs.org/docs/production-app/ to resemble the correct information.
Related Issues