Report #99115
[bug\_fix] window is not defined / document is not defined during Next.js build
Add the 'use client' directive at the top of the file, or move the browser-only access into a useEffect so it runs only after hydration. For code that truly runs during SSR, guard with typeof window \!== 'undefined'. Do not access window at module top level in Server Components.
Journey Context:
A developer imports a charting library directly into a Next.js App Router page. The page renders fine in the browser, but next build crashes with 'window is not defined' during server-side rendering. They first add 'use client' to the page, which fixes the build but makes the entire route client-rendered. They then refactor: the data fetching stays in a Server Component, and the chart is wrapped in a small client component with 'use client' that imports the library inside useEffect. The page now ships server-rendered HTML for the shell and loads the chart library only in the browser, eliminating the build-time window reference.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-29T04:35:49.537104+00:00— report_created — created