Code splitting uses dynamic import() to break one bundle into chunks that load when needed, so the first page ships less JavaScript to download, parse and execute. Split first by route, then by heavy, rarely-used features (editors, charts, modals, admin tools), and keep shared vendor code in stable chunks for caching. The failure modes are request waterfalls, too many tiny chunks, loading spinners on every click and broken deploys when old chunks disappear.
| Boundary | Example | Gain |
|---|---|---|
| Route | /checkout, /settings | Biggest, easiest win; frameworks do it automatically |
| Interaction-triggered feature | Rich-text editor, date picker, image cropper, a chart library | Large libraries load only when the user opens them |
| Below-the-fold or conditional UI | Comments, recommendations, a chat widget | Faster initial render |
| Role or feature flag | Admin panel, experiments | Most users never pay for them |
| Polyfills / legacy | Differential loading | Modern browsers skip them |
const Editor = lazy(() => import('./Editor'));
function Post() {
const [editing, setEditing] = useState(false);
return editing ? (
<Suspense fallback={<EditorSkeleton />}>
<Editor />
</Suspense>
) : (
<button
onPointerEnter={() => import('./Editor')} // warm the chunk on intent
onClick={() => setEditing(true)}>
Edit
</button>
);
}
Prefetching on hover, focus or idle hides most of the latency. import() is cached by the module system, so calling it twice is free.
rollup-plugin-visualizer, webpack-bundle-analyzer, source-map-explorer) on the production build to find big dependencies in the entry chunk.modulepreload, framework-level preloading).chunk-abc.js, which the new deploy deleted, and gets a 404 or ChunkLoadError. Keep old assets for some days, and catch dynamic-import failures by prompting a reload.index.ts that re-exports everything) can pull whole libraries into a chunk when tree shaking can't prove side-effect freedom.ChunkLoadError) as the classic production incidents.Spotted a mistake or a rights concern? Report an issue