Skip to content
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

perf(v2): reduce HTML payload by eliminating chunk-map #2118

Merged
merged 3 commits into from
Dec 13, 2019

Conversation

endiliey
Copy link
Contributor

@endiliey endiliey commented Dec 12, 2019

Motivation

Previously, we always embed a chunk map in the html to find the chunk assets of a webpack chunk name. As number of routes grew bigger, the html could grow bigger (although not fast, because its just text/string & text compression is really good)

The chunk mapping implementation was previously taken from "the blazingly fast SSG" implementation, so we're going to discard that but still preserve the prefetch functionality by leveraging webpack internal more. This PR is inspired after reading webpack/webpack#7056 and reading several parts of webpack still undocumented advanced internal code

The idea is that webpack runtime itself already know how to map chunk id to the relevant chunk assets.
https://github.com/webpack/webpack/blob/fe514dd975f1d2dd1546906a6f4c0a6b2a0e3761/lib/web/JsonpMainTemplatePlugin.js#L255

Webpack runtime itself build the script src by using

jsonpScriptSrc(chunkId);

So, we modify the webpack runtime to add an extra function called __webpack_require__.gca
that will allow us to get the corresponding chunk asset for a webpack chunk.
Pass it the chunkName or chunkId you want to load.
For example: if you have a chunk named "my-chunk-name" that will map to "0a84b5e7.c8e35c7a.js" as its corresponding output path
__webpack_require__.gca("my-chunk-name") will return 0a84b5e7.c8e35c7a.js

Why do we need to modify the webpack runtime ?
Because only that code part is aware of all the chunks and so on.

The key benefits of this PR is that our total payload is definitely slimmer, and scale better.
Build time should be slightly faster

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

Dev and prod should still be able to prefetch
2  simple debugging
production still working

Try it on netlify
image

On intersection observer, it creates link prefetch
prefetch

@endiliey endiliey added the pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient. label Dec 12, 2019
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Dec 12, 2019
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Dec 12, 2019

Deploy preview for docusaurus-preview ready!

Built with commit 7123eb3

https://deploy-preview-2118--docusaurus-preview.netlify.com

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Dec 12, 2019

Deploy preview for docusaurus-2 ready!

Built with commit 7123eb3

https://deploy-preview-2118--docusaurus-2.netlify.com

let chunkManifest;
const {path: defaultOutputPath, publicPath} = compiler.options.output;

// Build the chunk mapping
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bye old code

Copy link
Contributor

@lex111 lex111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, wow, it is awesome job! 👏

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solves our scalability problem. My favorite PR of the year 🎉

All hail the webpack guru 🙇

@endiliey endiliey merged commit daa71b2 into master Dec 13, 2019
@endiliey endiliey deleted the endi/rmchunkmap branch December 13, 2019 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants