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
- 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.
- 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/).*).
- 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.
- 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.
- 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 setup | vercel.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.json | Destination 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: false | Same rewrite. React Router’s docs: deploy build/client and “configure your host to direct all URLs to the index.html” |
| Next.js, SvelteKit, Nuxt | Don’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):
rewritesbecomes{"handle":"filesystem"}followed by your catch-all. Real files like/assets/index-Cd5haByk.jsare served first; only unknown paths fall through toindex.html.- A bare
routescatch-all compiles to just^/(.*)$ → /index.html, with no filesystem step. In our emulation, it answered every request withindex.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:
- Two-URL test. Open the same path on
your-app.vercel.appand on your custom domain. If only the custom domain fails, it’s DNS or domain config, not routing. - 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. - Framework Preset. Vite, not Other. Vercel’s guide says a preset of Other “can stop even a correctly written
vercel.jsonrewrite from taking effect.” - Typos.
rewriteinstead ofrewrites, orsrc/destinsiderewrites(those keys belong toroutes; rewrites usesource/destination). In our test, Vercel’s published JSON schema rejected both (rewrite: “must NOT have additional properties”;src/destin rewrites: “must have required property ‘source’”). - Whole site 404s, even
/. Not this bug. Check the Output Directory (Vite builds todist) and the preset.
Other hosts
| Host | SPA fallback |
|---|---|
| Netlify | A _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 Pages | Automatic: 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 Hosting | In firebase.json under hosting: "rewrites": [{ "source": "**", "destination": "/index.html" }]. |
| No server control at all | React 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.
/typono 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,/noperendered the app’s own not-found route. - Missing asset files get
index.htmltoo. In our emulation a request for a.jsfile that wasn’t in the build returnedindex.htmlwith 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/, outputsrun.out.txtandbrowser.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 withbase: '/'and withbase: './'. Eachvercel.jsonvariant was validated against Vercel’s published schema (openapi.vercel.sh/vercel.json) with Ajv and compiled with Vercel’s@vercel/routing-utils6.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;/dashboardand/dashboard/settings404 (404: NOT_FOUNDin the browser). rewrites/(.*)or/((?!api/).*):/dashboard,/dashboard/settingsand/dashboard?tab=1servedindex.html; Chrome rendered DASHBOARD and SETTINGS; the real JS bundle was served as JavaScript./api/hellogotindex.htmlwith/(.*)and a 404 (not rewritten) with/((?!api/).*).- Legacy
routescatch-all: compiled without a filesystem step; the JS bundle request gotindex.html(text/html). base: './'with a working rewrite:/dashboardrendered,/dashboard/settingsblank (its script URL/dashboard/assets/…returned HTML).rewrite(singular) andsrc/destinsiderewrites: rejected by the schema (schema-extra.out.txt); with no valid rewrite compiled, deep links 404.cleanUrls+/indexdestination:/dashboardservedindex.html;/index.htmlredirected (308) to/.
- No
- 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/apifunctions 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 /.
Want us to look at your app?
VibeCPR Rescue Reports are launching soon: we scan your repo and live app, and a human verifies every critical finding. Join the waitlist for early access and founder pricing. You'll also get Patch Notes, our weekly fixes newsletter.
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
- Vercel docs: Vite on Vercel (Using Vite to make SPAs)
- Vercel KB: Why is my deployed project giving a 404?
- Vercel docs: vercel.json (rewrites, routes, deprecated handle)
- Vercel docs: Rewrites
- Vercel docs: Configuring a build (Framework Preset, Root and Output Directory)
- React Router docs: Single Page App (SPA)
- Netlify docs: rewrites for history pushstate and single-page apps
- Cloudflare Pages docs: single-page application rendering
- Cloudflare Workers docs: single-page application static assets
- Firebase docs: Hosting rewrites
Related fixes
- AI agent pushed API keys to GitHub? git commit --no-verify skipped your pre-commit hook
- Fix a surprise AI API bill: rate limits and spend caps
- Your app is public to anyone with the link: a share link is not a login
- Triage Bolt's "RLS Policy Always True" warning: when it's fine and how to fix it
- Fix secrets leaked via Claude Code .claude/settings.local.json
- Fix "Your Cloud Firestore database has insecure rules" (Firebase email)
- Login lockout keyed only on email? Rate-limit by IP and account without locking users out
- Fix Lovable "Invalid API key" after rotating Supabase keys (anon, publishable, secret)
- Lovable won't publish because of security findings? What each finding means and how to fix it
- "Sign in with Google" lets anyone in: restrict who can use your app (allowlist, domain check, RLS)
- Fix Stripe sk_live keys left in MCP or agent config
- Fix Stripe webhook "No signatures found matching the expected signature for payload"
- Fix Supabase auth redirecting to localhost after deploy
- Fix Supabase Edge Function "blocked by CORS policy" (preflight)
- Fix Supabase "infinite recursion detected in policy for relation" (42P17) in RLS
- Form says success but no row in Supabase (or Stripe webhook returns 200 but writes nothing)
- Fix "new row violates row-level security policy for table" (Supabase)
- Supabase only returns 1,000 rows? Fix the silent max rows limit in Lovable and Bolt apps
- Fix Supabase "permission denied for table" (42501) on new tables after Oct 30, 2026
- Fix a Supabase public Storage bucket that lets anyone list or download your files
- Supabase Realtime leaking rows or not receiving changes: fix RLS and the publication
- Supabase RLS not working on the server? Your API uses the service_role or secret key
- Fix Supabase RLS policy USING (true) that makes a table publicly readable
- Your app's rules only live in the frontend: move them into Supabase
- Fix a Supabase SECURITY DEFINER function anyone can call through /rest/v1/rpc
- Fix Supabase .single() "JSON object requested, multiple (or no) rows returned" (PGRST116)
- Stop Supabase users from upgrading their own plan or making themselves admin
- Fix a Supabase view that bypasses RLS: security_invoker and revoking anon write grants
- Fix "Module not found: Can't resolve" on Vercel (works locally)
- Is my Vite dev server exposed? Check --host / server.host and patch the RCE (GHSA-p4f5-76q4-ffq7)