Code Splitting: React - Lazy and Suspence
Code Splitting: React - Lazy and Suspence
-> Bundling is the method of combining imported files with a single file. It
is done with the help of webpack, rollup etc.
-> With the help of code splitting lazy load can be implemented just using
the code which is currently needed.
-> The key change here is dynamic import, instead of a single component,
we are importing all of index.js and extracting the required component in
the promise callback.
-> When we build application for the production after implementing the
code splitting, we get many chunks of JS
-> After looking at this output, we might want to remove some of the last
few chunk since they’re really small.
->But we don’t know which split is causing which chunk to be created.
This is where chunk naming can helpful.
-> we can use a magic comment inside the import that tells Webpack to use
the given name for a specific chunk.
-> Once all the chunks are named, we can identify the splits that lead to
smaller chunks.
-> At this point, we can choose to remove or combine some of the smaller
chunks ( < 20-30KB in size), since the overhead of loading a 5KB chunk
might be higher than combining with one of the larger chunks.
-> Play around with different splits and see what works best for you.
“scripts”: {
“analyze”: “source-map-explorer build/static/js/main.*”,
}
-> Then to analyze the bundle run the production build then run the
analyze script.
Npm run build
npm run analyze