Report #103437
[bug\_fix] ReferenceError: window is not defined during next build, or hydration mismatch from gating JSX with typeof window.
Do not access window, document, or localStorage during the render pass. Read them inside useEffect or an event handler, or lazy-load the component with next/dynamic and ssr: false. Avoid using typeof window \!== 'undefined' to choose different rendered output.
Journey Context:
You reference localStorage to read a theme in a component and next build crashes with window is not defined. You then wrap the JSX in typeof window \!== 'undefined' ? : and now you get a hydration mismatch in the browser. The root cause is that Server Components and SSG prerender run in Node.js where window does not exist, and that JSX conditions make the server output differ from the first client render. The correct fix is to read the browser-only value after hydration in useEffect, or render the component only on the client via dynamic import with ssr: false. Both approaches guarantee the server-rendered HTML stays deterministic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:24:12.063964+00:00— report_created — created