Report #68794
[bug\_fix] window is not defined during server render
Guard browser-only globals with typeof window \!== 'undefined' before accessing them, or move the logic into a useEffect hook \(which only executes on the client\), or mark the component with 'use client' if it fundamentally requires the browser environment.
Journey Context:
You're building a landing page with a third-party analytics snippet that attaches to window.dataLayer. You add the initialization directly in the component body: window.dataLayer = window.dataLayer \|\| \[\];. When you run next build for your App Router project, it fails with 'ReferenceError: window is not defined'. You realize the App Router renders components on the server where browser globals don't exist. You try wrapping it in if \(typeof window \!== 'undefined'\), which fixes the build, but the analytics never initialize because the guarded code doesn't run again on the client after hydration \(the component tree is already mounted\). You learn that the correct pattern is to place browser-only initialization inside useEffect\(\(\) => \{ ... \}, \[\]\), which guarantees execution only on the client after the component mounts. After refactoring to useEffect, the build passes and the analytics load correctly in the browser.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:57:19.229168+00:00— report_created — created