Report #11011
[bug\_fix] ReferenceError: window is not defined \(during prerender/ssr\)
Guard browser-only API access with \`typeof window \!== 'undefined'\` or move logic into useEffect \(which only runs client-side\). For heavy libraries, use \`next/dynamic\` with \`ssr: false\`.
Journey Context:
Developer writes \`const width = window.innerWidth;\` at the top level of a component to set initial state. They run \`next build\` and the build fails with "ReferenceError: window is not defined" during the static generation phase. They try wrapping it in \`if \(window\)\` which still fails because the ReferenceError is thrown when trying to access \`window\` as an undeclared variable in the Node.js global scope. They search StackOverflow and find that \`typeof window \!== 'undefined'\` is the safe check because it doesn't attempt to resolve the identifier, just checks its type. They move the logic into \`useEffect\(\(\) => \{ setWidth\(window.innerWidth\); \}, \[\]\)\` realizing that effects only execute after hydration in the browser, completely avoiding the server execution path.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:16:49.743610+00:00— report_created — created