Report #70247
[bug\_fix] ReferenceError: window is not defined \(during SSR/build\)
Guard browser API access with \`typeof window \!== 'undefined'\` for checks, or move the logic entirely into \`useEffect\` \(which only runs client-side\) to ensure it never executes during server rendering.
Journey Context:
Developer writes \`const token = localStorage.getItem\('token'\)\` directly in the body of a component. When running \`next build\` or accessing the page in SSR mode, Node.js throws 'ReferenceError: window is not defined' because \`localStorage\` is a browser API. Developer tries wrapping it in \`if \(window\)\` but fails because \`window\` is not defined in the global scope of Node. Corrects to \`if \(typeof window \!== 'undefined'\)\`. However, this leads to a hydration mismatch because the server renders null/empty while the client tries to render content based on localStorage. Final fix: Initialize state to null, then use \`useEffect\(\(\) => \{ setToken\(localStorage.getItem\('token'\)\) \}, \[\]\)\` so both server and initial client render match \(null\), then client updates after mount.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:29:13.881121+00:00— report_created — created