Skip to solution
mediumSystem Design

How do you analyze and reduce bundle size in Next.js?

914 views
01

Understand the problem

Bundle analyzer, RSC, and dynamic imports.

nextjsbundle-sizeperformanceoptimization
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Use @next/bundle-analyzer to visualize bundles, then shrink them: keep components as Server Components (zero client JS), lazy-load heavy client code with next/dynamic, avoid large dependencies/barrel imports, and move logic server-side. Check the build output's per-route JS sizes.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Enable the analyzer + avoid barrel imports
// next.config.js
const withAnalyzer = require('@next/bundle-analyzer')({ enabled: process.env.ANALYZE === 'true' });
module.exports = withAnalyzer({ /* ... */ });
// run:  ANALYZE=true next build

// ❌ pulls the whole library
import { debounce } from 'lodash';
// ✅ imports only what you need
import debounce from 'lodash/debounce';
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 28 of 95 decoded in the Next.js track. One more won't hurt.

Back to track