How does Next.js handle dynamic imports?

Next.js supports standard JavaScript import() everywhere and adds next/dynamic, a wrapper around React.lazy and <Suspense> for components. Each dynamic import becomes a separate chunk that is downloaded only when the code runs, and next/dynamic can also skip server rendering with ssr: false.

import() (native)

  • Returns a promise for the module: const { format } = await import("date-fns").
  • Use it for libraries needed only after an event, or in Server Components to load modules lazily.
  • The bundler (Turbopack or webpack) creates a chunk per dynamic import.

next/dynamic

  • const Map = dynamic(() => import("./map")) creates a lazily loaded component.
  • loading: a component shown while the chunk downloads.
  • ssr: false: render only in the browser. Must be used inside a Client Component; using it in a Server Component is an error.
  • Named exports: dynamic(() => import("./charts").then((m) => m.BarChart)).

In Server Components

Dynamically importing a Server Component does not split client code (there is none); it only lazy-loads on the server. Client Components imported by it are still code-split.

Rules

  • The path in import() must be a static string or a template the bundler can analyze. Fully dynamic paths like import(userInput) are not supported.
  • Do not call dynamic() inside a component body; define it at module scope.
  • Turbopack 16.3 also supports import.meta.glob("./posts/*.md") for importing many files by pattern.

Code example

tsx
1// app/dashboard/widgets.tsx
2"use client";
3
4import dynamic from "next/dynamic";
5
6// Browser-only chart library: skip SSR, show a placeholder meanwhile
7const RevenueChart = dynamic(() => import("./revenue-chart"), {
8 ssr: false,
9 loading: () => <div className="h-64 animate-pulse rounded bg-slate-100" />,
10});
11
12// Named export from a module
13const HeatMap = dynamic(() => import("./maps").then((m) => m.HeatMap));
14
15export function Widgets({ data }: { data: number[] }) {
16 async function copyAsCsv() {
17 const { unparse } = await import("papaparse"); // native import() on demand
18 await navigator.clipboard.writeText(unparse(data.map((v) => ({ v }))));
19 }
20
21 return (
22 <section>
23 <RevenueChart data={data} />
24 <HeatMap />
25 <button onClick={copyAsCsv}>Copy CSV</button>
26 </section>
27 );
28}

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