Report #51965
[bug\_fix] ReferenceError: window is not defined / document is not defined / navigator is not defined during server rendering
Move browser-specific code into a \`useEffect\` hook \(which only executes on the client after hydration\) to ensure it doesn't run during server rendering. Alternatively, convert the file to a Client Component by adding the 'use client' directive at the top if the entire component depends on browser APIs.
Journey Context:
Developer writes \`const width = window.innerWidth;\` or \`localStorage.getItem\('key'\)\` directly in the body of a component in the App Router. During development with hot reloading, it appears to work because the code often runs on the client. However, on a hard refresh or during \`next build\`, it fails with 'window is not defined' because the component executes on the server during SSR/SSG. The developer tries to guard it with \`if \(typeof window \!== 'undefined'\)\`, but the error persists or hydration mismatches occur because React expects the server and client HTML to match. They eventually realize that Server Components cannot access browser globals at all. The correct fix is to either use \`useEffect\` \(which only runs on the client\) to access window and set state, or to add 'use client' to make the entire file a Client Component where window is available.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:43:05.071933+00:00— report_created — created