Report #96599
[bug\_fix] window is not defined or document is not defined during build
Move browser-only code into useEffect or a Client Component with 'use client'. Check for typeof window \!== 'undefined' as a guard if necessary, but prefer useEffect. Root cause: Server Components execute in Node.js runtime during build/request, where browser globals don't exist.
Journey Context:
Developer creates an analytics component that tracks window.scrollY. Writes directly in the component: \`const scrollPosition = window.scrollY;\`. In development, it works sometimes \(depending on caching\), but production build fails with 'window is not defined'. Developer is confused because it works in Pages Router. Realizes App Router uses Server Components by default. Tries checking \`if \(typeof window \!== 'undefined'\)\`, which fixes the build but causes hydration issues because server renders null/empty and client tries to hydrate. Final solution: Move logic to useEffect \(runs only on client\) or add 'use client' at top if the whole component needs to be client-side.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:43:37.951148+00:00— report_created — created