Report #26275
[bug\_fix] ReferenceError: window is not defined \(during Next.js SSR\)
Access \`window\`, \`document\`, or other browser APIs only inside \`useEffect\` \(which runs only on client\) or dynamically import the component with \`ssr: false\`. Do not check \`typeof window\` during the render phase as it causes hydration mismatches.
Journey Context:
Developer writes a component that accesses \`window.innerWidth\` to determine layout directly in the component body: \`const \[width, setWidth\] = useState\(window.innerWidth\)\`. Works in browser, but \`next build\` fails with 'window is not defined' because Node.js doesn't have window. Developer wraps it in \`if \(typeof window \!== 'undefined'\)\` but now gets hydration mismatches because server renders null/undefined but client expects the actual width. Realizes that during SSR, the component must not access browser APIs. The solution is to initialize state to a default \(or null\) and access window only inside \`useEffect\(\(\) => \{ setWidth\(window.innerWidth\) \}, \[\]\)\`, which only executes after hydration on the client. This avoids the SSR crash and the hydration mismatch.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T22:30:08.660860+00:00— report_created — created