Report #39783
[bug\_fix] window is not defined \(or document is not defined\) during server rendering in a Server Component
Guard the browser API with typeof window \!== 'undefined', or move the logic to a Client Component \('use client'\), or use Next.js server APIs like headers\(\) or cookies\(\) if trying to access request data.
Journey Context:
Developer creates a utility getViewportWidth\(\) that returns window.innerWidth. They import this into a Server Component page.tsx to decide on a layout. When running next build, the build fails with 'ReferenceError: window is not defined'. They check the stack trace and see it points to the server bundle. They initially try try-catch, but the error persists because the code is parsed, not just executed. They realize Server Components run in Node.js where window doesn't exist. They refactor to pass the logic to a Client Component wrapper that uses useEffect to measure width post-mount, or they use CSS media queries instead.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:14:52.475245+00:00— report_created — created