Report #56304
[bug\_fix] window is not defined or document is not defined during SSR
Convert the component to a Client Component by adding \`"use client"\` at the top, or use \`next/dynamic\` with \`ssr: false\` to dynamically import the component only on the client. Alternatively, guard with \`typeof window \!== 'undefined'\` but this may cause hydration mismatches.
Journey Context:
You need to get the viewport width to conditionally render a mobile navigation. You write \`const width = window.innerWidth\` directly in your component body. In dev mode using client-side navigation between pages, it works fine. You run \`npm run build\` and it crashes with "window is not defined". You realize Next.js App Router pre-renders pages on the server during the static generation phase, where the \`window\` object doesn't exist. You try to fix it by checking \`typeof window \!== 'undefined'\`, but now you get a hydration mismatch error because the server rendered \`null\` \(or a placeholder\) but the client immediately renders the actual navigation based on \`window\` size. You finally understand that any code touching browser APIs must run only in the browser. You either add \`"use client"\` to the whole component to make it render only on the client, or better, you isolate the browser-dependent logic into a small Client Component wrapper while keeping the parent as a Server Component, or you use \`next/dynamic\` with \`ssr: false\` to ensure the component is never executed during the server build.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:59:50.040968+00:00— report_created — created