Report #60789
[bug\_fix] window is not defined or localStorage is not defined \(ReferenceError during SSR\)
Move the browser API access into a useEffect hook \(which only runs on the client\), or use dynamic import with ssr: false, or check typeof window \!== 'undefined' \(though useEffect is preferred to avoid hydration mismatches\).
Journey Context:
Developer writes const token = localStorage.getItem\('token'\); directly in the body of a React component. In a Next.js App Router project, this component might be a Server Component by default, or even in a Client Component, this code runs during the SSR phase. The Node.js server environment does not have localStorage or window, so it throws 'ReferenceError: localStorage is not defined' during the build or server render. Developer tries wrapping the code in if \(typeof window \!== 'undefined'\), which prevents the error but can cause hydration mismatches if the server renders something different from the client. The correct fix is to put localStorage access inside useEffect, which guarantees it only runs in the browser after hydration. Alternatively, they can use next/dynamic with ssr: false to completely disable server-side rendering for that specific component.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T08:31:27.031154+00:00— report_created — created