Full answer
Bundle size affects download, parse and execution time, so it shows up in load speed and in INP on slow devices. Work in a loop: measure, change, measure again.
Measure. Visualize the output with webpack-bundle-analyzer, rollup-plugin-visualizer, source-map-explorer or the framework's own report (for Next.js, @next/bundle-analyzer). With esbuild or Vite, the metafile or rollup output lists every module and size. Look for large libraries, duplicated packages (two versions of the same library), whole locale or icon sets, and code that is used on one page but loaded everywhere. Compare gzip or brotli sizes, because those are transferred. Use Lighthouse or the Coverage panel to see how much loaded code is unused at startup.
Split. A dynamic import('./Chart.js') returns a promise and tells the bundler to create a separate chunk. React.lazy(() => import('./Chart')) wraps it as a component that suspends until the chunk loads, and needs a <Suspense fallback> ancestor. Natural split points are routes (first priority), modals and drawers, tabs the user may never open, and heavy libraries used on interaction (a markdown editor, a PDF viewer). Lazy components must be the default export of the imported module. Place Suspense boundaries so a fallback does not replace the whole page, and give the fallback a similar size to avoid layout shift. Handle chunk load failures (offline, or a new deployment removed the old chunk) with an error boundary that offers a reload.
Prefetch. Start loading the chunk on hover, on focus, when idle or when the link scrolls into view, by calling the import function early. Bundlers support hints such as /* webpackPrefetch: true */. Framework routers do this for links.
Shrink what remains. Use ES module versions of libraries so tree shaking works, and set sideEffects: false in your own packages when safe. Import directly (date-fns/format, or the library's documented path) instead of through large barrel files, unless the library supports tree shaking well. Replace heavy libraries with lighter ones or platform APIs (Intl instead of moment). Check polyfills and browserslist targets. Move non-critical third-party scripts out of the main bundle. Server Components keep server-only code out of the client bundle entirely.
Guardrails. Add a size budget in CI (for example with size-limit or bundler warnings), review bundle changes in pull requests, and watch real-user metrics.
The example uses esbuild's API to build the same code with and without splitting.
import { build } from 'esbuild';
import { mkdtemp, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
const dir = await mkdtemp(path.join(tmpdir(), 'split-'));
await writeFile(path.join(dir, 'greet.js'), "export const greet = (name) => 'Hello ' + name;\n");
await writeFile(path.join(dir, 'chart.js'), 'export const data = ' + JSON.stringify('x'.repeat(30000)) + ';\n');
await writeFile(
path.join(dir, 'main.js'),
"import { greet } from './greet.js';\nconsole.log(greet('Asha'));\nexport const loadChart = () => import('./chart.js');\n"
);
async function bundle(splitting) {
const result = await build({
entryPoints: [path.join(dir, 'main.js')],
bundle: true,
format: 'esm',
splitting,
minify: true,
outdir: path.join(dir, splitting ? 'split' : 'single'),
metafile: true,
write: false,
});
return Object.entries(result.metafile.outputs).map(([file, info]) => ({
name: path.basename(file).replace(/-[A-Z0-9]+\./, '.'),
kb: Math.round(info.bytes / 1000),
}));
}
const single = await bundle(false);
const split = await bundle(true);
console.log('without splitting: ' + single.map((f) => f.name + ' ' + f.kb + ' kB').join(', '));
console.log('with splitting: ' + split.map((f) => f.name + ' ' + f.kb + ' kB').join(', '));This ran esbuild on the box with a small synthetic project. The chart module contains 30 kB of data, standing for a heavy library. With splitting, the entry stays tiny and the chart becomes its own chunk, loaded only when loadChart runs. Output of the run: without splitting: main.js 30 kB with splitting: main.js 0 kB, chart.js 30 kB
Key points
- Measure with an analyzer or metafile before changing anything, and compare compressed sizes.
- Split by route first, then by rarely used features and heavy libraries, with import() and React.lazy inside Suspense.
- Handle chunk load failures and use similar-size fallbacks.
- Prefetch likely chunks on hover, focus or idle time.
- Reduce the remainder with tree shaking, direct imports, lighter libraries and CI size budgets.
Common mistakes
- Optimizing without looking at an analyzer.
- Lazy-loading everything, including small above-the-fold components, and creating loading waterfalls.
- Forgetting a Suspense boundary around lazy components.
- Ignoring chunk load errors after a new deployment.
- Importing a whole library for one function.
Follow-ups
- How does tree shaking work and what breaks it?
- How would you prefetch a route chunk when the user hovers a link?
- How do React Server Components change client bundle size?
Tips
- Start with measurement. It prevents guesswork.
- Say route-level splitting comes first, and mention chunk load errors.
- Give one concrete library replacement, such as Intl instead of moment.