Report #68578
[bug\_fix] window is not defined / document is not defined during SSR build
Add the \`"use client"\` directive at the very top of the file \(before imports\) to mark the component as a Client Component, ensuring it only renders in the browser. Alternatively, dynamically import the library with \`ssr: false\` if the component is heavy.
Journey Context:
Developer builds a page with a charting library or tries to access \`localStorage\` in a component. During \`next build\` or on initial SSR, the app crashes with 'window is not defined'. They try wrapping the access in \`if \(typeof window \!== 'undefined'\)\`, but the error persists because the import statement at the top of the file executes the library code immediately, which references \`window\`. They realize that in the Next.js App Router, components are Server Components by default. To force the code to run only on the client, they must add \`"use client"\` at the top of the file. This tells Next.js to treat the file as a Client Component boundary, skipping it during server rendering.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:35:39.960999+00:00— report_created — created