Skip to content
Sandeep Kumar
Updated 5 min read

What is actually in your Next.js JavaScript bundle

Shipping less JavaScript starts with knowing what is in it. Reading the build output, running the analyzer, and the four imports that usually explain the size.

Sandeep Kumar, Full-stack EngineerSandeep KumarFull-stack Engineer
On this page
  1. Start with the build output
  2. Then open the chunk
  3. The four imports that usually explain it
  4. The structural lever: where the code runs
  5. Confirm what you shipped
  6. Where to start, and where to stop

"Ship less JavaScript" is advice nobody argues with and almost nobody acts on, because the first question — less of what? — has no obvious answer. The bundle is a number in the build output and a folder of hashed filenames, and neither tells you which import to go and delete.

This is how to turn that number into a short list of names.

Start with the build output

Before installing anything, run a production build and read the route table.

code
Route (app)                              Size  First Load JS
┌ ○ /                                  1.2 kB         112 kB
├ ○ /blog                              1.8 kB         113 kB
├ ● /blog/[slug]                       4.1 kB         148 kB
└ ○ /about                               940 B         111 kB
+ First Load JS shared by all                         106 kB

Two columns, and the second is the one that matters. Size is the code unique to that route. First Load JS is everything the browser must download and execute before the route is interactive, meaning the route's own code plus the shared chunk.

The shared line at the bottom is the quiet one, and the one I check first. It is paid on every route, so a heavy dependency imported in a root layout costs far more than the same dependency imported in a single page. If that number is large, no amount of per-page work will rescue you.

So the table asks the question for you: one fat route, or a fat baseline?

Then open the chunk

Once you know which one to look inside, @next/bundle-analyzer shows what is in it.

next.config.tsts
import withBundleAnalyzer from "@next/bundle-analyzer";

const analyze = withBundleAnalyzer({ enabled: process.env.ANALYZE === "true" });

export default analyze({
  // your config
});
bash
ANALYZE=true npm run build

It opens a treemap where area is size. The habit worth building is ignoring everything small. In every bundle I have opened, three or four blocks explain most of the weight and the remaining hundred are noise that will eat an afternoon if you let them. Name the big blocks, then go find the import that pulled each one in.

The four imports that usually explain it

Date libraries come first. A full-featured one with locale data can outweigh your application code. If you format a handful of dates, Intl.DateTimeFormat is in the browser already and costs nothing:

ts
const formatted = new Intl.DateTimeFormat("en-IN", {
  day: "numeric", month: "short", year: "numeric",
}).format(new Date(iso));

Icon packs imported from the index are next. import { ChevronDown } from "some-icons" reads like one icon and can resolve to a module re-exporting thousands of them. Import the specific path, or list the package in optimizePackageImports:

next.config.tsts
export default {
  experimental: { optimizePackageImports: ["some-icons", "some-ui-kit"] },
};

Utility libraries pulled in for two functions are the third. Importing a whole package to call debounce and groupBy is a trade most teams would decline if the cost were visible on the invoice. Import the two from their own paths, or write them, since both are under ten lines.

And a heavy component rendered rarely is the fourth. A chart library, a rich-text editor, a map. These belong behind a dynamic import so they load when the thing is actually used:

tsx
import dynamic from "next/dynamic";

const Chart = dynamic(() => import("./Chart"), {
  ssr: false,
  loading: () => <div className="h-64" />,  // reserve the space, or you trade bytes for layout shift
});

One constraint on that snippet: ssr: false is only allowed in a Client Component, so the dynamic import lives in the interactive part of the tree rather than in the server page that renders it.

That loading placeholder is load-bearing, not decoration. Swapping a lazily-loaded component into a page with no reserved height is exactly how a bundle fix turns into a Cumulative Layout Shift problem, and then you have traded one metric for another.

The structural lever: where the code runs

Everything above is tuning. The real decision in the App Router is what runs on the server at all.

A Server Component's code never reaches the browser. The markdown parser, the syntax highlighter, the database client, the date formatting: all of it executes during the render and what ships is HTML. The moment a component is marked "use client", it and everything it imports become part of the client bundle.

That makes "use client" the most expensive line in a Next.js codebase, and the usual mistake is putting it too high up the tree.

tsx
// Costly: the whole page, its imports and its children become client code
"use client";
export default function Page() { /* … */ }
tsx
// Better: the page stays on the server, and one small leaf is interactive
export default function Page() {
  return (
    <article>
      <Prose html={html} />        {/* server: parser never ships */}
      <CopyButton text={code} />   {/* "use client" lives in here */}
    </article>
  );
}

Push the boundary down to the leaves that genuinely need state, effects or event handlers, and the rest of the tree, along with every library it touches, stays on the server. It is the same boundary discipline that keeps the hero out of hydration's way.

Confirm what you shipped

A bundle fix that is not verified has a habit of coming back.

Re-read the route table after building again and compare First Load JS per route. If the shared line did not move, the dependency is still somewhere shared, however many dynamic imports you added on top.

Then read the network panel on a cold load. Throttle to a slow connection, disable cache, and watch what JavaScript is requested before the page becomes interactive. The analyzer shows what exists. The network panel shows what the browser actually fetched for this route, which is the only one of the two numbers a visitor experiences.

Next's own defaults also shift between versions, and bundling behaviour and image handling both changed in ways that are easy to miss. The Next.js 16 notes collect the ones that caught me. A size change you did not cause is usually one of those.

Where to start, and where to stop

Bundle work goes wrong when it starts with the folder of hashed filenames. Start with the table. Find the route, find the chunk, find the three names that explain it.

In practice the fix is almost always one of four things. A date library replaced by Intl. An index import narrowed to a path. A rarely-used component moved behind dynamic. A "use client" pushed further down the tree.

Everything after that is rounding, and rounding is not worth a sprint.

Frequently asked

What is a good JavaScript bundle size for a Next.js page?

There is no official threshold, but the practical guidance is to keep First Load JS under roughly 150-200 KB compressed for a content page. What matters more than the number is the trend: a page that grows 30 KB in a sprint is a regression worth explaining, whether it started at 90 KB or 300 KB.

How do I see what is inside my Next.js bundle?

Start with the route table that next build prints, which gives per-route and shared First Load JS. For the contents, add @next/bundle-analyzer and run a build with ANALYZE=true; it opens a treemap where the area of each block is its size, so the expensive dependencies are the ones you can see without squinting.

Does using Server Components reduce bundle size?

Yes, and it is the largest single lever in the App Router. A Server Component renders to HTML on the server and sends no component code to the browser, so any library used only there: a markdown parser, a date formatter, a database client: never reaches the client bundle at all.

Why did my bundle grow after adding one small import?

Usually a barrel file. Importing one named export from a package index can pull the whole index into the graph when the package is not tree-shakeable or the import is re-exported through a chain of files. Import from the specific path instead, or add the package to optimizePackageImports in next.config.ts.

Found this useful?Share on XShare on LinkedIn

Related reading