Report #53707
[bug\_fix] ReferenceError: window is not defined during Next.js SSR
Guard the window access with \`typeof window \!== 'undefined'\` before using it, or move the logic into a \`useEffect\` hook which only executes after hydration on the client. Root cause: Server-side rendering executes in a Node.js environment where the browser \`window\` global object does not exist.
Journey Context:
Developer needs to get the user's screen width to conditionally render a mobile menu. They write \`const isMobile = window.innerWidth < 768;\` directly in their component body. When they refresh the page, the server crashes with 'ReferenceError: window is not defined'. They are confused because the code works in the browser console. They realize that Next.js renders the component on the server first. They try to check if window exists using a ternary, but if written incorrectly it still throws. The correct approach is either checking \`typeof window \!== 'undefined'\` before accessing it, or better, using \`useEffect\` to set the state after mount, ensuring the server render doesn't depend on browser-only APIs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T20:38:38.117215+00:00— report_created — created