What is the purpose of the next/dynamic function?

next/dynamic lazy-loads a React component in its own JavaScript chunk, so it is downloaded only when rendered. It combines React.lazy and <Suspense> with Next.js extras: a loading placeholder, the option to disable server-side rendering, and support for named exports.

Signature

dynamic(() => import("./Component"), { loading?, ssr? })

Use cases

  • Heavy components that are not needed immediately: charts, maps, rich-text editors, code editors, video players.
  • Components shown after interaction: modals, drawers, tabs, "show more" sections.
  • Browser-only libraries that touch window on import: ssr: false.
  • Conditional features: load an admin-only toolbar only for admins.

Rules

  • Call dynamic() at the top level of a module, not inside a component.
  • The import path must be static (analyzable by the bundler).
  • ssr: false is only allowed in Client Components. In a Server Component, wrap the dynamic call in a small "use client" file.
  • When importing a Server Component dynamically, only the Client Components inside it are lazy-loaded on the client.

Pitfalls

  • Lazy-loading small components adds a request for little gain.
  • ssr: false removes the content from the initial HTML, which can hurt SEO and LCP. Use it only for components that genuinely cannot render on the server.
  • The loading component should reserve space to avoid layout shift.

Code example

tsx
1// app/contact/map.tsx
2"use client";
3
4import dynamic from "next/dynamic";
5
6// Leaflet touches window at import time: render only in the browser
7export const OfficeMap = dynamic(() => import("./leaflet-map"), {
8 ssr: false,
9 loading: () => <div style={{ height: 320 }} aria-busy="true">Loading map…</div>,
10});
11
12// app/contact/page.tsx (Server Component)
13// import { OfficeMap } from "./map";
14// export default function Contact() {
15// return (
16// <main>
17// <h1>Visit us</h1>
18// <OfficeMap lat={28.61} lng={77.21} />
19// </main>
20// );
21// }

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