← All guides

AI-Built App 404 on Refresh or Blank After Deploy? Fix It

LovableBoltv0

You exported a Lovable or Bolt app, connected the repo to Vercel or Netlify, and the homepage looks fine. Then you refresh /dashboard (or share that link) and the host returns 404. Or the page loads blank. Or import.meta.env.VITE_SOMETHING is undefined in production even though it worked locally.

Those three symptoms share a theme: the deploy succeeded, but the host is serving a static SPA (or a misconfigured build) without the rewrite and env setup the app needs. This guide covers the fixes in that order. Related: export your Lovable or Bolt app to GitHub. Tool pages: /fix/lovable, /fix/bolt, /fix/v0.

Why SPAs 404 on refresh

A single-page app builds to one HTML file (index.html) plus JavaScript. Client-side routers (React Router, Vue Router, and similar) invent paths like /settings in the browser. The host never created a file at /settings.

Clicking inside the app works because the router handles the change without a new request for HTML. Refreshing, or opening the deep link directly, sends a real request for /settings. Vercel and Netlify look for a file at that path, find none, and return 404.

Vercel’s own 404 troubleshooting guide states this clearly: Vercel “resolves every URL as a server-side filesystem path by default”, which is correct for Next.js-style frameworks and wrong for Vite or Create React App SPAs unless you add a catch-all rewrite. Netlify documents the same pattern as a rewrite that serves index.html with status 200 so the URL in the bar stays the same (Netlify: rewrites and proxies).

This rewrite is for static SPAs. Do not paste it into a Next.js, SvelteKit, or Nuxt project that already has its own routing. Vercel’s guide says those frameworks should use their native routing; a catch-all index.html rewrite can break them. For build failures on a v0 / Next.js deploy, use v0 app deploy errors on Vercel instead.

Newer Lovable projects that use TanStack Start need a host that runs server code, not only a static SPA fallback. Lovable’s hosting notes and the export guide cover that case. If your package.json lists @tanstack/react-start, fix the framework preset and server env vars before you chase SPA rewrites.

Fix 1: Vercel rewrite (vercel.json)

At the repository root (committed to Git, not gitignored), add:

{
  "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}

That pattern is the one Vercel documents for SPA fallback in Why is my deployed project giving a 404? and in the vercel.json rewrites docs. Existing static assets still win over the rewrite when a real file matches.

If the same project also serves /api routes from the same deploy, exclude them so API calls are not rewritten to HTML:

{
  "rewrites": [{ "source": "/((?!api/).*)", "destination": "/index.html" }]
}

Two traps from Vercel’s guide:

  1. vercel.json must be at the repo root and tracked in Git. If it never reaches the deployment, rewrites silently do nothing.
  2. Framework Preset set to “Other” can stop a correct rewrite from applying. Prefer the Vite (or matching) preset when the project is a Vite SPA.

After you commit vercel.json, trigger a new deployment. Then open a nested route directly and refresh it.

If you set cleanUrls: true, Vercel says the rewrite destination must drop the .html extension (use /index instead of /index.html). Most exported Lovable/Bolt Vite apps do not need cleanUrls for this fix.

Fix 2: Netlify _redirects (or netlify.toml)

For Netlify, put a _redirects file in the publish directory (often dist for Vite), with:

/*    /index.html   200

Netlify treats status 200 as a rewrite: the address bar stays on /settings, and the server returns index.html (Netlify: history pushState and single-page apps). Keep this rule last if you have other redirects.

Equivalent in netlify.toml at the repo root:

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

Netlify’s default “shadowing” behavior still serves a real file when one exists, so JS and CSS under /assets keep working. You usually want that for SPAs.

Confirm the publish directory in the Netlify site settings matches where _redirects lands after npm run build. A _redirects file that stays only in the repo root and never copies into dist will not run.

Blank page: console, base path, missing env

A blank white page after deploy is a different problem from a host 404. Open the browser developer console and the Network tab before you change DNS or rewrites.

Common causes on exported Vite apps:

  • JavaScript failed to load. Wrong base in Vite config, or the app is hosted under a subpath while assets are requested from /. Vite exposes import.meta.env.BASE_URL from the base config; a mismatch shows as 404s on /assets/... in the Network tab.
  • A runtime exception on startup. Often Cannot read properties of undefined after a missing env var (next sections).
  • Wrong output directory. The host is serving an empty or old folder. Compare the build output (dist for typical Vite) with the host’s Output Directory setting.

If the Network tab shows index.html as 200 but the JS bundle as 404, fix the base path or asset paths first. If index.html itself is 404 on every nested route, go back to the rewrite sections above.

VITE_ prefix and build-time env vars

Vite only exposes environment variables that start with VITE_ to client code, via import.meta.env (Vite: env variables and modes). A variable named SUPABASE_URL without the prefix is undefined in the browser even when it exists on the host.

Vite also statically replaces those values at build time. Changing a VITE_ value in the host dashboard does nothing until you rebuild and redeploy.

Checklist:

  1. Name public client values with the VITE_ prefix (for example VITE_SUPABASE_URL, VITE_SUPABASE_PUBLISHABLE_KEY).
  2. Set the same names in Vercel Settings → Environment Variables or Netlify site env settings, for the environment you are testing (Production vs Preview).
  3. Redeploy after every change.
  4. Confirm the built JS contains the expected public values (search the bundle, or log import.meta.env.VITE_... behind a temporary debug flag).

On Vercel, variables are scoped per environment (Vercel environment variables). A key added only for Preview will be missing in Production. Vercel also states that env changes apply only to new deployments.

For Next.js apps from v0, the public prefix is NEXT_PUBLIC_ instead of VITE_. That case is covered in v0 deploy errors.

Redeploy after changing env vars

After you add or edit env vars:

  1. Trigger a new production (or preview) deployment. Do not expect an old deployment to pick up the change.
  2. Hard-refresh the browser, or open the site in a private window, so you are not looking at a cached empty shell.
  3. If you use preview deployments, confirm you are reading the Preview env group when testing a PR URL.

If the value is still wrong after a redeploy, check for typos in the name (VITE_SUPABASE_URL vs VITE_SUPABASE_ANON_URL), and confirm the build command actually runs vite build (or your framework’s build) rather than serving an old artifact.

Secrets that must not use VITE_

Vite’s docs warn that VITE_* values “are bundled into your source code at build time” and “should not contain sensitive information such as API keys” (Vite: protecting secrets). Anyone can download your JS and read them.

Never put these in a VITE_ (or NEXT_PUBLIC_) variable:

  • Supabase service role key
  • Stripe secret key
  • Database passwords
  • OpenAI / other AI provider secret keys
  • Any token that can write billing, delete data, or bypass row-level security

Public, publishable values (Supabase anon/publishable key with RLS, Stripe publishable key) can use the client prefix. Secrets belong in server-only code: Edge Functions, server routes, or a backend the browser never sees. For a broader checklist, see vibe-coded app security checklist.

A short order of checks

  1. Nested route refresh → 404: add the Vercel or Netlify SPA rewrite, redeploy, retest.
  2. Confirm the project is a static SPA (Vite/React), not Next.js/TanStack Start with its own server routing.
  3. Blank page → open the console and Network tab; fix asset 404s or startup exceptions next.
  4. undefined env in the client → VITE_ prefix, correct host environment, then redeploy.
  5. Secrets → remove from VITE_, move to server-only config, rotate anything that was exposed.

If the deploy is still broken after the rewrite and env checklist, a senior engineer can review the host settings and the repo together. See /fix/lovable, /fix/bolt, or /fix/v0.

FAQ

Why does my homepage work but other pages 404?

The homepage is index.html. Other paths exist only in the client router until you add a host rewrite that serves index.html for those URLs (Vercel vercel.json or Netlify _redirects with status 200).

Why are env vars undefined in production?

On Vite apps, only names starting with VITE_ are exposed to the browser, and they are baked in at build time. Missing prefix, wrong host environment (Preview vs Production), or no redeploy after a change are the usual causes (Vite env docs).

Do I need to redeploy after adding an env var?

Yes. Host dashboards do not patch old builds. Vercel documents that env changes apply to new deployments only.

Is it safe to put my API key in a VITE_ variable?

Only if that key is meant to be public (for example a publishable anon key behind RLS). Secret keys must never use VITE_ or any other client-exposed prefix.

Why is the page blank with no obvious error?

Check the browser console and failed network requests for JS/CSS. A missing env var often throws on first render; a wrong base path 404s the bundle. Host 404 on the HTML itself points back to the SPA rewrite.

Comments

Add a comment

Generated for you. Click to type your own.

Required. Never shown on the page. Privacy policy.

Basic markdown: **bold**, *italic*, `code`, lists, quotes, and fenced code blocks. HTML shows as plain text.

Your draft stays on this device until you post.