Skip to content
>_VibeUnstuck

404 on refresh of a client route

Last verified:

The fix

A 404 on refresh means the Vite app works from /, then a reload of /dashboard misses index.html. Rewrite unknown paths to index.html on the host.

  1. 1

    Confirm it is a client route, not a missing build

    Open / and click to the page. If that works and only refresh or a pasted URL fails, the router is fine and the host is missing a fallback. If / itself 404s, the publish directory is wrong. Vite's production build is usually dist.

  2. 2

    On Vercel, add a rewrite at the repository root

    Commit vercel.json at the root of the repo. Vercel's SPA example rewrites every path to /index.html. The filesystem still wins for real files. If the app also has API routes, the Vercel 404 guide says to exclude them with a negative lookahead, or those calls are rewritten to index.html too. The same guide says this rewrite is for a Vite or similar SPA, not for Next.js, which has its own router. A Framework Preset of Other can ignore a correct file.

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

    On Netlify, add a 200 rewrite in _redirects

    Put this last in public/_redirects (or the publish directory). Status 200 rewrites the request and leaves the URL alone. Netlify will not shadow a file that exists, so assets stay assets. Put any /api/* proxy rule above this line. A 301 or 302 here would change the address bar and is the wrong status.

    /*    /index.html   200
  4. 4

    On Apache or Hostinger, fall back to index.html and skip API and asset prefixes

    Apache's FallbackResource /index.html returns that file when the URL maps to nothing, and still serves files that exist. It also falls back for a missing /api or /assets URL, which turns a bad script request into HTML and a different error (Unexpected token '<'). Skip those prefixes, then fall back only when the path is not a real file or directory. Do not add this rule to a Next.js export that already has one HTML file per route.

    RewriteEngine On
    RewriteRule ^(api|assets)/ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.html [L]

Why it happens

A Vite app (the usual Lovable or Bolt export) is a single index.html plus JavaScript. The router changes the URL in the browser. The first load works because the host served /, which is index.html. A refresh of /dashboard asks the host for a file named dashboard. There is no such file, so the host returns 404.

The host has to serve index.html for those client routes and leave real files alone. Vercel's project configuration documents a catch-all rewrite to /index.html for a single-page app, and says the filesystem is checked before rewrites. Netlify documents /* /index.html 200 as a rewrite (status 200, not a browser redirect) and says an existing file is not shadowed by default. Apache's FallbackResource serves a default URL for requests that do not map to a file, and leaves existing files, including images and CSS, alone.

The next error you'll probably hit

Once this is fixed, the usual follow-up is Unexpected token '<' is not valid JSON. Unexpected token '<' is not valid JSON: res.json() read HTML, usually a 404 or index.html. Check the status and URL first. A <!DOCTYPE body is not JSON.

Prompt to paste back into your AI tool

Paste this above the error.

A Vite (or other client-rendered) app loads at / and returns 404 when I refresh or open /dashboard directly. The host is <Vercel, Netlify, or Apache>.

Do not add a Node server only to serve index.html. Do not rewrite API or asset paths to index.html. Do not swallow the 404 in an empty catch. Do not disable RLS, and do not put secrets in NEXT_PUBLIC_ or VITE_.

1. Confirm the path works when I click from the home page and fails on refresh.
2. For Vercel, add a vercel.json rewrite to /index.html at the repo root, excluding ^/api/. Say if this project is Next.js, because Next should not use that SPA rewrite.
3. For Netlify, add `/* /index.html 200` as the last _redirects rule.
4. For Apache, skip /api and /assets, then rewrite to index.html only when the request is not a real file or directory.
5. Show only that config file.

Common questions

+The home page works. Why does /dashboard fail only after a refresh?

The first visit served index.html, and the client router painted /dashboard. The refresh asks the host for a file. There is no dashboard file in a typical Vite build, so the host 404s until it falls back to index.html.

+I added the fallback and now JavaScript files throw Unexpected token '<'. Why?

The fallback is serving index.html for missing asset URLs. Exclude /assets (and /api) from the rewrite so a missing file stays a 404 instead of an HTML document with a 200.

Related errors