-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby): enable granular chunks #22253
feat(gatsby): enable granular chunks #22253
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking awesome!
if (/@babel(?:\/|\\{1,2})runtime|core-js/.test(modulePath)) { | ||
// TODO this needs rework, this is buggy as hell | ||
if ( | ||
/@babel(?:\/|\\{1,2})runtime|core-js|react|react-dom|scheduler|prop-types/.test( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Ward! Left some suggestions below but looks really good.
docs/docs/production-app.md
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// All css is bundled into one stylesheet | |
// All CSS is bundled into one stylesheet |
docs/docs/production-app.md
Outdated
}, | ||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// A chunk should be at least 20kb to be considered into splitChunks | |
// A chunk should be at least 20kb before using splitChunks |
docs/docs/production-app.md
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Minify css by using cssnano (https://cssnano.co/) | |
// Minify CSS by using cssnano (https://cssnano.co/) |
docs/docs/production-app.md
Outdated
@@ -62,6 +93,14 @@ This bundle is produced from [production-app.js](https://github.com/gatsbyjs/gat | |||
|
|||
This contains the small [webpack-runtime](https://webpack.js.org/concepts/manifest/#runtime) as a separate bundle (configured in `optimization` section). In practice, the app and webpack-runtime are always needed together. | |||
|
|||
##### 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
docs/docs/production-app.md
Outdated
|
||
##### 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the right docs info!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added just one question :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome! Excited to get this in!
Published in |
(very late to the party, but 💜 🙏 🎉) |
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