Report #6707
[bug\_fix] window is not defined or localStorage is not defined in Next.js App Router
Add the "use client" directive at the top of the file before imports. Root cause: Server Components execute in a Node.js environment during SSR/SSG where browser globals like window and localStorage do not exist; only Client Components run in the browser.
Journey Context:
Developer creates a LoginButton component that reads a token from localStorage to show user state. They place it in app/dashboard/page.tsx. On hard refresh, the page crashes with "localStorage is not defined" in the terminal logs. They check the browser DevTools and see the error originates from the server bundle \(rsc://\). Confused, they search the error and discover that App Router defaults to Server Components. They add "use client" at the very top of the file, which forces the component into the client bundle where localStorage is available, resolving the crash.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:44:46.379931+00:00— report_created — created