Bundle analysis and lazy loading
Find heavy client code with next analyze, split it out with next/dynamic and dynamic import(), and know where bundle growth usually comes from.
The use client boundaryComposing Server and Client Components
Bundle analysis and lazy loading
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Only code under a `"use client"` boundary and what it imports ships to the browser; Server Components do not add to the client bundle.
- `next analyze`, added in 16.1 and Turbopack-only, shows modules by route and environment with full import chains, and `--output` saves a snapshot.
- Next.js 16 removed per-route JS sizes from `next build` output, and `@next/bundle-analyzer` is the webpack-era alternative.
- `next/dynamic` and dynamic `import()` load code only when needed; `ssr: false` works only inside Client Components.
Why bundle size is the first thing to measure
Every byte of client JavaScript costs three times: downloading it, parsing it, and running it. On a fast laptop that is invisible. On a mid-range phone it is the difference between a page that responds to a tap and one that does not. Server Components help, since their code never ships to the browser at all, but anything under a "use client" boundary, and everything it imports, goes into the bundle.
The trap is that bundle growth is silent. Nobody decides to add 300 KB. One engineer imports a date library for a single format call, a chart library for one widget, and a month later the home page is heavy. You cannot fix what you cannot see, so the first step is measuring.
Seeing the bundle: next analyze
Next.js 16.1 added a bundle analyzer built into the CLI and Turbopack’s module graph:
npx next analyze
It builds the app for analysis (it does not produce a deployable build), then starts a local server with an interactive view. You can filter by route, switch between client and server modules, and, most usefully, click any module and see the import chain that pulled it in. Finding that moment is in your home page is half the job. Seeing that it arrived through utils/format.ts, imported by a banner component, is the other half.
To keep a record, --output writes the analysis to .next/diagnostics/analyze without starting the server. The docs suggest copying that directory before a refactor so you can compare afterward, and Next.js 16.4 added next analyze export to stream a saved snapshot as JSON Lines for scripts.
One thing changed in 16.0 that older tutorials miss: the per-route JavaScript size table was removed from next build output. If a video shows a “First Load JS” column, that is the old output. Use the analyzer instead. The older @next/bundle-analyzer package still exists for projects running on webpack; it works through an ANALYZE=true build, while next analyze is Turbopack-only.
Lazy loading with next/dynamic
Once you know what is heavy, the fix is to not load it until needed. next/dynamic is Next.js’s wrapper around dynamic import() and React’s lazy loading (see code splitting and lazy loading for the React side):
"use client";
import dynamic from "next/dynamic";
const Chart = dynamic(() => import("./Chart"), {
loading: () => <p>Loading chart…</p>,
});
export function Dashboard({ show }: { show: boolean }) {
return show ? <Chart /> : null;
}
The chart’s code is split into its own file and fetched only when Chart first renders. For a component below the fold or behind a tab, the saving lands directly on first load.
I measured this on a throwaway Next.js 16.4.6 app whose home page had a Markdown preview built on the marked library, shown only after a button click. Imported statically, the home page’s HTML referenced 638,543 bytes of uncompressed script and one chunk of about 58 KB held marked. Behind dynamic(() => import("./markdown-preview")) the same page referenced 592,843 bytes and no initial chunk contained marked. The sizes are raw bytes from .next/static/chunks, not gzip, and the gap is smaller than the chunk because chunk boundaries shifted, so treat it as direction, not a benchmark.
Two rules from the docs trip people up. ssr: false only works in Client Components. If you try it in a Server Component, you get an error; move the dynamic call into a file marked "use client". Use ssr: false for things that genuinely need the browser, such as a map library that touches window, not as a general optimization. And named exports need .then((m) => m.Name) to pick the export.
Loading a library on demand
You do not always need a component to lazy-load. A library used only after a user action can be imported inside the event handler. The docs example loads fuse.js for fuzzy search only when someone types in the search box, so people who never search never pay for it.
Where bundles actually come from
When you find a large dependency, the usual causes are: a client component importing a library it only uses on the server side of the same file, one "use client" high in the tree that drags a whole subtree into the bundle, and barrel imports that pull in an entire icon or utility library to use one function. The fix is often to move the boundary down so less code is under it, which the composition lessons earlier in this roadmap cover.
What the video covers
Blazity’s “How to Use Dynamic Imports in Next.js to Reduce Bundle Size” is a 5-minute chaptered video from July 2025: it analyzes a demo app, lazy-loads a chart, loads a map in a more advanced way, and compares the build results. It does not show an analyzer tool, so this lesson supplies next analyze. Ben Haig’s 10-minute video from 2023 is under “deeper”; it demonstrates @next/bundle-analyzer, which is the webpack-era workflow.
Where this fits
Bundle size feeds into the metrics Google measures. Next: Core Web Vitals in Next.js.
Check yourself
What is the most useful thing the bundle analyzer shows beyond module size?
The import chain, which tells you why a heavy module is in a bundle and so which file to change.
Why does `ssr: false` fail in a Server Component?
`ssr: false` is only supported in Client Components, so the `next/dynamic` call has to move into a file marked `"use client"`.
When is a dynamic `import()` inside an event handler a good fit?
When a library is needed only after a user action, such as a search library loaded when the user starts typing, so people who never trigger it never download it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.