VibeCPR

← All fixes

Deploy · Routing

Fix "404: NOT_FOUND" on refresh in a React/Vite app on Vercel (vercel.json rewrite)

Updated (first published ) · Last verified · Applies to: Vercel, Vite, React Router, Lovable, Bolt, Cursor, Netlify, Cloudflare Pages

TL;DR fix: Your single-page app has one real page, index.html; React Router draws /dashboard in the browser. On a refresh, Vercel looks for a file called /dashboard, finds none, and returns 404. Fix: create vercel.json in the project root (next to package.json) with {"rewrites":[{"source":"/(.*)","destination":"/index.html"}]}, commit it, and redeploy. If you have /api functions, use "/((?!api/).*)" as the source. Use rewrites, not legacy routes. Not for Next.js, SvelteKit or Nuxt.

Steps

  1. Confirm it's an SPA routing issue. The home page loads and clicking links works, but refreshing or opening a URL like /dashboard directly shows 404: NOT_FOUND. If the home page also 404s, it's not this: check the Framework Preset and Output Directory. If only your custom domain 404s and the .vercel.app URL works, it's DNS.
  2. Create vercel.json in the project root. Next to package.json (in a monorepo, in the folder set as Vercel's Root Directory). Add one catch-all rewrite to /index.html. If you have serverless functions under /api, exclude them with source /((?!api/).*).
  3. Commit the file. Make sure vercel.json is tracked by git and not in .gitignore. Vercel shows no error if the file is missing, so the rewrite just silently doesn't apply.
  4. Check the framework preset and base path. In Vercel's project settings the Framework Preset should be Vite, not Other. In vite.config, base should be '/' (the default), not './', or nested deep links like /dashboard/settings load a blank page.
  5. Redeploy and test deep links. Open https://your-app.vercel.app/dashboard and a nested route like /dashboard/settings directly, refresh both, and check that /api routes still return JSON.

The file

vercel.json in your project root (next to package.json):

{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "rewrites": [
    { "source": "/((?!api/).*)", "destination": "/index.html" }
  ]
}

The (?!api/) part keeps /api/* serverless functions out of the fallback. If you don’t have any, "/(.*)" is fine. The $schema line is optional; it gives you autocomplete and catches typos in editors that support it.

Vercel’s guide explains the cause: “Vercel resolves every URL as a server-side filesystem path by default… It is wrong for single-page applications (SPAs) built with Vite, Create React App, or similar tools.” And its Vite page: if your Vite app is a single-page app, “deep linking won’t work out of the box” (Vercel docs).

Which variant do you need?

Your setupvercel.json
Plain Vite + React Router, no /api folder{"rewrites":[{"source":"/(.*)","destination":"/index.html"}]}
Vite app plus Vercel functions in /api{"rewrites":[{"source":"/((?!api/).*)","destination":"/index.html"}]}
"cleanUrls": true in vercel.jsonDestination without the extension: "destination": "/index" (Vercel docs)
Old file with "routes": [{ "src": "/(.*)", "dest": "/index.html" }]Replace it with the rewrites version above
React Router framework mode with ssr: falseSame rewrite. React Router’s docs: deploy build/client and “configure your host to direct all URLs to the index.html”
Next.js, SvelteKit, NuxtDon’t add this. Use the framework’s own routing config

Rewrites vs routes: why the old snippet gives you a blank page

Stack Overflow answers from a few years ago use routes. Vercel’s vercel.json docs now list the route property handle as deprecated and say: “Use rewrites instead, which checks the filesystem by default.”

That filesystem check is the whole point. We compiled both configs with Vercel’s own routing library (@vercel/routing-utils):

  • rewrites becomes {"handle":"filesystem"} followed by your catch-all. Real files like /assets/index-Cd5haByk.js are served first; only unknown paths fall through to index.html.
  • A bare routes catch-all compiles to just ^/(.*)$ → /index.html, with no filesystem step. In our emulation, it answered every request with index.html, including the JavaScript bundle, so the page loads HTML where the script should be and stays blank.

The blank-page trap: base: './'

Some templates and AI-generated configs set base: './' in vite.config. Then index.html loads ./assets/index-….js, relative to the current URL. On /dashboard that’s /assets/… and works. On /dashboard/settings it becomes /dashboard/assets/…, which doesn’t exist, so the catch-all answers with index.html and the browser gets HTML instead of JavaScript. In our headless-Chrome test, /dashboard rendered and /dashboard/settings was a blank page. Fix: remove base (the default is /) or set base: '/', then rebuild.

Still 404 after adding vercel.json?

Vercel’s 404 guide lists the usual suspects. In order:

  1. Two-URL test. Open the same path on your-app.vercel.app and on your custom domain. If only the custom domain fails, it’s DNS or domain config, not routing.
  2. Is the file deployed? It must be committed, not in .gitignore, and in the folder Vercel builds (Project Settings → Build and Deployment → Root Directory, if you set one). Vercel gives no error when it’s missing.
  3. Framework Preset. Vite, not Other. Vercel’s guide says a preset of Other “can stop even a correctly written vercel.json rewrite from taking effect.”
  4. Typos. rewrite instead of rewrites, or src/dest inside rewrites (those keys belong to routes; rewrites use source/destination). In our test, Vercel’s published JSON schema rejected both (rewrite: “must NOT have additional properties”; src/dest in rewrites: “must have required property ‘source’”).
  5. Whole site 404s, even /. Not this bug. Check the Output Directory (Vite builds to dist) and the preset.

Other hosts

HostSPA fallback
NetlifyA _redirects file with /* /index.html 200 in the deployed folder (in Vite, put it in public/ so the build copies it to dist), or the same rule in netlify.toml. Netlify serves files that exist before the rewrite (“shadowing”).
Cloudflare PagesAutomatic: if no file matches, Pages serves the root index.html. A top-level 404.html turns this off, so delete it if deep links 404.
Cloudflare Workers (static assets)In your Wrangler config: assets.not_found_handling = "single-page-application".
Firebase HostingIn firebase.json under hosting: "rewrites": [{ "source": "**", "destination": "/index.html" }].
No server control at allReact Router’s hash routing (createHashRouter) puts the route after #, which browsers don’t send to the server. It works anywhere but gives you /#/dashboard URLs.

Two side effects to know about

  • Unknown URLs now return 200 with your app. /typo no longer gets Vercel’s 404; your app decides what to show. Add a catch-all route (path: "*") that renders a “Page not found” screen. In our test, /nope rendered the app’s own not-found route.
  • Missing asset files get index.html too. In our emulation a request for a .js file that wasn’t in the build returned index.html with a 200, not a 404. If a page breaks with a MIME-type or “Failed to fetch dynamically imported module” error right after a deploy, an old tab asking for an old chunk is one possible cause; a reload fixes it.

Don’t “fix” deep links by serving production with npm run dev, vite or vite preview. Those are dev tools, and an exposed Vite dev server on an affected version can be taken over with a single request. See Is my Vite dev server exposed?

Prompt for your AI tool

Deep links 404 on Vercel (404: NOT_FOUND) in our Vite + React Router app. Add vercel.json in the project root
(next to package.json) with a single "rewrites" catch-all to /index.html that excludes /api/:
{"rewrites":[{"source":"/((?!api/).*)","destination":"/index.html"}]}. Remove any legacy "routes" catch-all.
Make sure vite.config has no relative base (base must be '/' or unset), add a path:"*" not-found route,
make sure vercel.json is committed and not gitignored, and tell me to set the Vercel Framework Preset to Vite.
Don't change other routing code.

What we verified

  • Tested locally (Nov 30, 2026; research/vercel-404-test/, outputs run.out.txt and browser.out.txt). This emulates Vercel’s routing; it is not Vercel’s edge network: a Vite 8.4.0 + React 19.3.1 + React Router 8.5.3 app, built with base: '/' and with base: './'. Each vercel.json variant was validated against Vercel’s published schema (openapi.vercel.sh/vercel.json) with Ajv and compiled with Vercel’s @vercel/routing-utils 6.6.1; a small Node server walked the compiled routes (filesystem step, then rewrites), and headless Chrome 156 loaded the pages.
    • No vercel.json: / 200; /dashboard and /dashboard/settings 404 (404: NOT_FOUND in the browser).
    • rewrites /(.*) or /((?!api/).*): /dashboard, /dashboard/settings and /dashboard?tab=1 served index.html; Chrome rendered DASHBOARD and SETTINGS; the real JS bundle was served as JavaScript. /api/hello got index.html with /(.*) and a 404 (not rewritten) with /((?!api/).*).
    • Legacy routes catch-all: compiled without a filesystem step; the JS bundle request got index.html (text/html).
    • base: './' with a working rewrite: /dashboard rendered, /dashboard/settings blank (its script URL /dashboard/assets/… returned HTML).
    • rewrite (singular) and src/dest inside rewrites: rejected by the schema (schema-extra.out.txt); with no valid rewrite compiled, deep links 404.
    • cleanUrls + /index destination: /dashboard served index.html; /index.html redirected (308) to /.
  • Docs-based, not tested (fetched Nov 30, 2026; list in research/keys-and-vercel-404-refresh-2026-11-30.md): how Vercel’s edge actually behaves, the Framework Preset “Other” effect, Root Directory, how Vercel matches /api functions before rewrites, and the Netlify, Cloudflare and Firebase settings. We didn’t deploy to Vercel, Netlify, Cloudflare or Firebase for this page.
  • Our reading, not documented: that AI-generated configs sometimes set base: './'; the stale-chunk explanation for post-deploy MIME errors.

When to get help

Get help if you're on Next.js (where this rewrite is wrong), if API routes start returning HTML after the change, if deep links show a blank white page instead of a 404, or if the whole site 404s even at /.

FAQ

Why do I get 404: NOT_FOUND on Vercel when I refresh my React app?

Vercel resolves every URL as a file path by default. A Vite or Create React App single-page app only has index.html, and React Router handles routes in the browser, so a direct request to /dashboard has no file to serve. Add a catch-all rewrite to /index.html in vercel.json at the project root, commit it, and redeploy.

What should vercel.json contain for a Vite React app?

{ "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }] }. If the project has Vercel functions under /api, use "/((?!api/).*)" as the source so API calls aren't answered with index.html. If cleanUrls is true, the destination is "/index" without .html.

Should I use routes or rewrites in vercel.json?

Use rewrites. Vercel's vercel.json docs mark the route property handle as deprecated and say to use rewrites instead, "which checks the filesystem by default": real files such as /assets/index-abc.js are served first and only unknown paths go to index.html. A legacy routes catch-all without a filesystem step sends your JavaScript requests to index.html too, which shows up as a blank page.

My vercel.json rewrite is there but I still get 404. Why?

Usually one of four things: the file isn't committed (or is in .gitignore), it isn't in the project root that Vercel builds (check Root Directory), the Framework Preset is Other instead of Vite, or the key is misspelled (rewrite instead of rewrites fails Vercel's JSON schema). Also check whether .vercel.app works but your custom domain 404s; that's DNS, not routing.

Deep links load a blank white page instead of a 404. What's wrong?

Often a relative base in vite.config (base: './'). The HTML then loads ./assets/index.js, which on /dashboard/settings resolves to /dashboard/assets/index.js, a file that doesn't exist, so the catch-all answers with index.html instead of JavaScript. In our test, /dashboard rendered and /dashboard/settings was blank. Set base to '/' (the default) and rebuild.

Does this apply to Netlify, Cloudflare Pages or Firebase Hosting?

The cause is the same, the file differs. Netlify: a _redirects file with /* /index.html 200. Cloudflare Pages: serves index.html for unknown paths automatically unless your build has a top-level 404.html. Cloudflare Workers static assets: not_found_handling = "single-page-application". Firebase Hosting: a rewrite with source "**" and destination /index.html.

Should I add this rewrite to a Next.js app?

No. Next.js has its own routing on Vercel; Vercel's guide says routing for Next.js belongs in next.config.js and that vercel.json rewrites can conflict with it. The same guide says SvelteKit and Nuxt should use their own SPA settings, not a vercel.json catch-all.

Sources

Related fixes