What is code splitting in Next.js?

Code splitting breaks your JavaScript into smaller chunks so each page loads only the code it needs. Next.js does it automatically per route and per shared dependency, and Server Components remove component code from the client bundle entirely. You can split further with next/dynamic and dynamic import().

Automatic splitting

  • Per route: visiting /blog does not download the code for /dashboard.
  • Shared chunks: code used by many routes (React, shared components) goes into common chunks that are cached across navigations.
  • Per layout segment in the App Router.
  • CSS is split per route as well.
  • Turbopack (default in Next.js 16) handles chunking; since 16.4 the Turbopack runtime ships in a single chunk shared by all routes.

Server Components

The biggest reduction: components without "use client" never become client JavaScript, and neither do their dependencies (markdown parsers, date libraries, SDKs).

Manual splitting

  • next/dynamic(() => import("./Chart")) for heavy Client Components.
  • await import("library") inside an event handler for code used only after an interaction.
  • Avoid importing whole libraries; optimizePackageImports automatically trims barrel imports for many popular packages.

Prefetching works with it

<Link> prefetches the chunks and RSC payload for routes in the viewport, so split code is ready before the click.

Measuring

Use next analyze (or next build --analyze, Turbopack) or @next/bundle-analyzer (webpack) to see chunk sizes. Next.js 16 removed the "First Load JS" column from next build output because it was inaccurate with RSC; use Lighthouse or real-user metrics instead.

Code example

tsx
1// app/editor/page.tsx (Server Component)
2import { EditorShell } from "./editor-shell";
3
4export default function EditorPage() {
5 // Nothing from this file ships to the browser
6 return <EditorShell />;
7}
8
9// app/editor/editor-shell.tsx
10"use client";
11
12import dynamic from "next/dynamic";
13import { useState } from "react";
14
15// Separate chunk, downloaded only when rendered
16const RichEditor = dynamic(() => import("./rich-editor"), { loading: () => <p>Loading editor…</p> });
17
18export function EditorShell() {
19 const [editing, setEditing] = useState(false);
20
21 async function exportPdf() {
22 const { jsPDF } = await import("jspdf"); // loaded only on click
23 new jsPDF().text("Hello", 10, 10).save("doc.pdf");
24 }
25
26 return (
27 <div>
28 <button onClick={() => setEditing(true)}>Edit</button>
29 <button onClick={exportPdf}>Export PDF</button>
30 {editing && <RichEditor />}
31 </div>
32 );
33}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app