Next.jsMedium

How do you disable SSR for a component using next/dynamic?

Pass { ssr: false } to next/dynamic inside a Client Component: const Editor = dynamic(() => import("./editor"), { ssr: false }). The component is skipped during server rendering, its loading fallback is sent in the HTML, and it renders only in the browser after its chunk loads.

When it is justified

  • Libraries that access window, document or navigator at import time (some map, chart, editor or canvas libraries).
  • Components whose output depends entirely on browser state, where any server output would mismatch.
  • Very heavy widgets you do not want to render on the server at all.

Rules in the App Router

  • ssr: false is not allowed in Server Components and produces an error. Create a small "use client" wrapper file that calls dynamic(..., { ssr: false }) and import that wrapper from the Server Component.
  • Provide a loading component that reserves the same space, to avoid layout shift.

Alternatives (often better)

  • Fix the import: move browser access into useEffect so the component can SSR.
  • useSyncExternalStore with a server snapshot for browser values.
  • Render after mount with an isClient state flag for small pieces.
  • use(browser()) / use(io()) with Cache Components to defer browser-dependent values out of the static shell.

Trade-offs

The content is not in the initial HTML: crawlers may not see it, LCP can suffer, and users wait for JavaScript. Use it for interactive widgets, not for main content.

Code example

tsx
1// app/notes/editor-client.tsx - the wrapper must be a Client Component
2"use client";
3
4import dynamic from "next/dynamic";
5
6export const NotesEditor = dynamic(() => import("./monaco-editor"), {
7 ssr: false, // monaco needs window at import time
8 loading: () => <div className="h-96 rounded border bg-slate-50" aria-busy="true" />,
9});
10
11// app/notes/page.tsx (Server Component)
12import { NotesEditor } from "./editor-client";
13// import dynamic from "next/dynamic";
14// const Bad = dynamic(() => import("./monaco-editor"), { ssr: false }); // Error in a Server Component
15
16export default async function NotesPage() {
17 const initial = await fetch("https://api.example.com/notes/1").then((r) => r.text());
18 return (
19 <main>
20 <h1>Notes</h1>
21 <NotesEditor initialValue={initial} />
22 </main>
23 );
24}

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