UnbreakMyApp / Fix guides

Blank white screen after deploy: "window is not defined" or "localStorage is not defined"

A v0, Cursor or Bolt Next.js app works in the editor, then the live site is blank or shows a server error. The code read a browser-only API while Next.js was still rendering on the server.

Building a new app instead? Try Bolt (20% off with our link) (affiliate link).

Why it happens

In the Next.js App Router, pages are Server Components by default. The server builds the HTML before any browser exists, so window, document and localStorage are not there. Code that reads them at the top of a file, or during the first render, throws ReferenceError: window is not defined (or localStorage is not defined). In production that often becomes a blank page or a generic error, with the real message only in the Vercel function logs or the browser console.

Adding "use client" is not enough on its own. Next.js still pre-renders Client Components on the server for the first load, then hydrates them in the browser. A render that checks typeof window, or that prints new Date() or Math.random(), can also mismatch and show "Hydration failed" or "Text content does not match server-rendered HTML".

This is a Next.js problem (v0, Cursor, and Bolt when the project is Next.js). A classic Lovable Vite app does not server-render, so a blank screen only after login is usually the Supabase auth listener, which is a different fix.

How to fix it, step by step

  1. Open the live site, then Vercel → the deployment → Functions or Runtime Logs, and the browser console. Copy the first ReferenceError or hydration message and the file name in the stack.
  2. Search the project for window, document, localStorage and sessionStorage. Anything that runs while the component is rendering (or at the top of the file, outside a function) has to move.
  3. Put "use client" at the top of the component file, above the imports. Keep the first render identical on server and client: start state at a fixed value such as null, and read localStorage inside useEffect.
  4. If a library touches window the moment it is imported (maps, charts, some editors), load it with next/dynamic and { ssr: false }. That call has to live in a Client Component. Next.js rejects ssr: false in a Server Component.
  5. Do not "fix" a mismatch with suppressHydrationWarning except for a single element that must differ, such as a timestamp. Next.js documents that it only covers that element.
  6. Redeploy and load the page in a private window. The first paint can be a short placeholder; it should not throw, and the saved value should appear after hydration.

Paste-ready prompt for Lovable

The live Next.js app is blank or throws "window is not defined" / "localStorage is not defined" / a hydration error. Please:
1. Find every read of window, document, localStorage or sessionStorage, and every library that uses them on import.
2. For app code: add "use client" where interactivity or browser APIs are required, but read those APIs only inside useEffect (or an event handler). The first render must return the same markup on the server and the client. Use a placeholder until the effect runs.
3. For a browser-only library: load it from a Client Component with next/dynamic(() => import('...'), { ssr: false }). Do not put ssr: false in a Server Component.
4. Remove typeof window checks from the render path; they cause hydration mismatches.
5. Do not use suppressHydrationWarning except on one element whose text must differ (a clock). Do not change other features. Show the diff.

FAQ

I added "use client" and it still crashes. Why?

Client Components are still pre-rendered on the server. Move the browser call into useEffect, or skip SSR for that component with next/dynamic.

My Lovable app is Vite, not Next.js, and the screen is blank after login.

That is a different failure: the Supabase auth listener deadlock. This guide is for the server-render crash in Next.js.

Still broken?

Get a free diagnosis by email within 24 hours. Fixed or refunded.

Get a free diagnosis

Or get the weekly fix digest:

📬 This Week in Broken Apps: the most common Lovable/Supabase breaks and how to fix them. Free, weekly, unsubscribe anytime.

Free: the Vibe-Code Fix Checklist.

Sources