Next.js Bundle Size: Analyzing and Cutting Chunk Bloat
By Nayan Kyada · · 5 min read
Part of Next.js Performance
Next.js bundle size analysis is one of the highest-ROI performance tasks you can do — it takes an afternoon, and the wins compound across every page. Here is how the process works, using the kinds of offenders that typically show up on an e-commerce site. The numbers below are illustrative — yours will differ.
Setting up a bundle analyzer
On Next.js 16.1 and later, the default Turbopack bundler has a built-in (experimental) analyzer — no install needed:
npx next experimental-analyzeIf your project builds with Webpack, use the @next/bundle-analyzer plugin instead. Install the package and wire it into next.config.ts.
// next.config.ts
import type { NextConfig } from 'next';
import withBundleAnalyzer from '@next/bundle-analyzer';
const withAnalyzer = withBundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
openAnalyzer: true,
});
const nextConfig: NextConfig = {
// your existing config
};
export default withAnalyzer(nextConfig);Then run:
ANALYZE=true next build --webpackThis opens treemap reports in your browser for the client and server bundles. The client treemap is the one to fix first because it is what your users download. Every rectangle you hover over shows the parsed size; sort by largest first and write them down before touching any code.
What typically shows up on the client treemap
Three offenders come up again and again:
1. date-fns (41 kB parsed) — the code imported from the top-level package rather than the specific functions:
// before — pulls the full lib
import { format, parseISO } from 'date-fns';On older setups this could pull in far more of date-fns than you use. Current Next.js optimizes date-fns imports by default (it's on the built-in optimizePackageImports list), and date-fns v3+ is ESM-first — so if it still shows up large, check that you're on a current version of both. A fix like this typically takes it from tens of kB down to a few.
2. Icon or utility barrels — libraries that re-export hundreds of modules from one index file:
// barrel import — can pull in far more than the four icons used
import { ShoppingCart, Search, User, ChevronDown } from 'some-icon-library';If the bundler can't tree-shake the barrel, the whole library can end up as one solid block in a shared chunk that ships on every route. Next.js already handles the popular ones — lucide-react, react-icons/*, @heroicons/react and others are optimized by default. For a library that isn't on that list, add it to optimizePackageImports so you keep the readable import syntax and only load the modules you use:
// next.config.ts (addition)
experimental: {
optimizePackageImports: ['some-icon-library'],
},3. An unsplit shared chunk — a large shared chunk that bundles together things like Radix Dialog, Radix Dropdown, a rich-text editor used only on the account page, and the full Zod library. If none of these are dynamically imported, the bundler has no reason to split them.
A rich-text editor (@tiptap/react + extensions) used on a single route behind authentication is the easiest target. Wrapping it with next/dynamic and ssr: false moves it out of the shared chunk. Note that ssr: false is only allowed in Client Components, so the dynamic import lives in a small client wrapper:
// app/account/profile/bio-editor-lazy.tsx
'use client';
import dynamic from 'next/dynamic';
export const BioEditor = dynamic(() => import('@/components/bio-editor'), {
ssr: false,
loading: () => <div className="h-32 animate-pulse rounded-md bg-zinc-100" />,
});Then import BioEditor from that wrapper into the (server) page and render it as usual.
Zod in a shared form-validation module that every page touches is another common one. Rather than lazy-loading Zod itself, audit which schemas are actually needed on the server vs the client. Moving server-only validation into route handlers or Server Actions takes Zod out of the client bundle entirely — it then runs only on the server.
Measuring the result
Re-run the analyzer after each change and compare shared-chunk size and first-load JS per route, then check Lighthouse (or field data) for TBT and INP. Less JS parsing on load means the main thread is free sooner, which is what INP rewards.
What to check on every new project
Run the analyzer before adding any significant library, not just when you notice slowness. The three patterns that create recurring bloat:
- Barrel-import libraries: check the analyzer for icon or utility packages that aren't on Next.js's default
optimizePackageImportslist, and add them. - Shared utility modules: anything imported in more than five places ends up in the vendor chunk. Audit whether each utility actually needs to be client-side.
- Heavy components behind auth or toggles: rich-text editors, drag-and-drop, chart libraries, and map components should almost always be lazy-loaded. They are never on the critical path.
The analyzer output is also useful in scope conversations with clients. A screenshot of a 100 kB+ library block is far more persuasive than explaining tree-shaking in the abstract.
Frequently asked questions
01How do I set up @next/bundle-analyzer in Next.js?
On Next.js 16.1+ with the default Turbopack bundler, run `npx next experimental-analyze` instead — it's built in. `@next/bundle-analyzer` is the Webpack plugin: wrap your `next.config.ts` export with `withBundleAnalyzer({ enabled: process.env.ANALYZE === 'true' })`, then run `ANALYZE=true next build --webpack`. Either way, fix the client bundle first since it's what ships to users. Sort by largest size and start there.
02Why does importing icons from lucide-react (or react-icons) bloat my Next.js bundle?
On current Next.js it usually doesn't — `lucide-react`, `react-icons/*`, `@heroicons/react` and `date-fns` are on the list of packages Next.js optimizes by default via `optimizePackageImports`, so named imports from the package index only load the icons you use. If an icon or utility library that isn't on that list shows up as a big block in the analyzer, add it to `experimental.optimizePackageImports` in `next.config.ts`.