Overview
Code splitting ships several chunks instead of one bundle, using dynamic import() so the browser downloads only what the current route or interaction needs. Split at route boundaries first (Next.js does this automatically), then lazy-load heavy components that most visitors never open, such as editors, charts, and maps.
Rules
- In React, pair
React.lazy(() => import("./X"))with a suspense-boundary. In Next.js,next/dynamicwraps both for Client Components. - Do not split tiny components; extra requests create waterfalls that cost more than they save. Measure with a bundle analyzer before and after.
- Stable vendor chunks keep cache hits when app code changes.
- Split chunks still hydrate on the client (hydration).
const ChartEditor = React.lazy(() => import("./ChartEditor"))
<Suspense fallback={<Spinner />}>
<ChartEditor />
</Suspense>Related
- react:
lazyand Suspense. - nextjs: route-level splitting and
next/dynamic. - tree-shaking: removes dead code before splitting.
- suspense-boundary: renders the fallback while a chunk loads.
- hydration: chunks hydrate after loading.