Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

404 on page refresh in a React or Vue app (SPA): how to fix it

A 404 when you refresh a page or open a link like /about in a React Router or Vue Router app means the static host looked for a file at /about, found none, and never loaded index.html, where the router lives. Fix it on the host by rewriting every route to /index.html (Netlify _redirects, Vercel rewrites, nginx try_files), or switch to hash routing. The price of a rewrite: unknown paths stop being 404s unless the app shows its own.

By Livops team, the people who build and run LivopsUpdated

What people see, on a site that works until they press refresh or open a link someone sent:

GET https://example.com/about 404 (Not Found)

The home page loads, links inside the app work, but reloading /about, /users/42 or /settings shows the host’s “page not found”. This page explains why a single-page app (React Router, Vue Router and the like) does this on a static host, gives the fix for Netlify, Vercel and nginx, the hash-routing alternative, what each fix costs, and what Livops does on its own.

Why does a refresh give a 404?

A built single-page app is one HTML file, index.html, plus scripts and styles. The router lives in those scripts. createBrowserRouter in React Router manages the path with the browser’s history.pushState and history.replaceState[7], so moving from / to /about inside the app changes the address bar without asking the server for anything.

A refresh is different. The browser sends GET /about to the server, and a static host looks for a file at that path. There is none: /about exists only inside the JavaScript. The host answers 404, and index.html, the only thing that could have drawn the page, is never loaded.

Vue Router’s docs describe exactly this: with HTML5 history mode, users get a 404 if they open an address like https://example.com/user/id directly in the browser[5]. React Router’s docs say the same from the other side: to deploy any SPA you configure the host to send all URLs to the index.html of the build, some hosts do that by default and some do not, and 404s on valid routes usually mean the host needs configuring[6].

There are two ways out: make the server answer every route with index.html (a rewrite), or keep the route out of the request (hash routing).

Fix 1: rewrite every route to /index.html

A rewrite serves index.html with status 200 while the address bar keeps /about, so the router starts and draws the right page. It is not a redirect: a 301 or 302 would send the browser to /, and the router would lose the path.

Netlify

Put a _redirects file in the folder Netlify publishes. With Vite, put it in public/: files there are copied to the root of the output folder during the build[11].

/*    /index.html   200

Netlify’s docs give this exact rule for single-page apps that use pushState, and say it is typically the last rule when there are others[1]. The same rule in netlify.toml[1]:

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

Netlify does this well: by default a rule cannot shadow a file that exists, so /assets/app.js is still served as the script even under a /* rule[1]. To force a rule over existing files you add ! after the status[1].

Vercel

In vercel.json at the project root, the rewrite Vercel’s docs give for a single-page app[2][3]:

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

Vercel’s Vite page says a Vite app built as an SPA has no deep linking out of the box, and that this file is how to enable it[3]. As on Netlify, real files win: Vercel checks the filesystem before applying rewrites[2].

nginx

On your own server, try_files checks the listed files in order, serves the first one that exists, and if none does makes an internal redirect to the last parameter[4]. The block Vue Router’s docs give for nginx[5]:

location / {
  try_files $uri $uri/ /index.html;
}

$uri serves a real file such as /assets/app.js, $uri/ a folder, and everything else falls through to /index.html. Because files are tried first, scripts and images keep working.

React Router in framework mode

With ssr: false in react-router.config.ts, React Router builds an index.html you can serve as a static SPA[6]. You deploy build/client, and the host still needs the rewrite above; React Router’s own example is the same /* /index.html 200 line in _redirects[6].

Fix 2: hash routing

If you cannot change the host’s configuration, put the route after a #. That part of the URL is not sent to the server[9], so every request is for /, and index.html always loads.

In React Router, createHashRouter creates a router that keeps the path in the URL hash[8]; it is the data-mode counterpart of createBrowserRouter. In declarative mode, <HashRouter> stores the location in the hash portion of the URL so it is not sent to the server[9].

import { createHashRouter } from "react-router";

const router = createHashRouter([
  { path: "/", Component: Home },
  { path: "/about", Component: About },
]);
// pass it to <RouterProvider router={router} />, as with createBrowserRouter

In Vue Router, use createWebHashHistory instead of createWebHistory[5]:

import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

The cost: addresses become https://example.com/#/about, links shared before the switch stop matching, and Vue Router’s docs say hash mode has a bad impact on SEO[5]. Use it when the host gives you no rewrites; otherwise prefer Fix 1.

The cost of a rewrite: real 404s

Once every unknown path gets index.html, the server cannot tell a real route from a typo. /abuot now answers 200 with your app, and search engines and link checkers see a page, not an error. Vue Router’s docs warn about this: the server no longer reports 404 errors, because all not-found paths serve index.html[5].

The fix is in the app: a last, catch-all route that renders a “Not found” page.

  • Vue Router: the docs’ own catch-all is { path: '/:pathMatch(.*)', component: NotFoundComponent }[5].
  • React Router: a route path ending in /* (a splat) matches any characters after it, including other /[10], so a last route with path * catches everything the others did not.

The status code is still 200; the visitor sees the right message, but a crawler sees a page. Keep that in mind for a site that lives on search traffic, and see the next section for a host that answers 404 itself.

How to confirm the fix

Check the deployed site, not your machine. Vite’s default app type is spa, which uses an SPA fallback in the dev server, and vite preview is set up the same way[11], so locally the bug does not show. Against the real host, check three addresses:

curl -sI https://example.com/about          | head -1   # 200: the rewrite works
curl -sI https://example.com/assets/nope.js | head -1   # 404: missing files still fail
curl -s  https://example.com/about | grep -o "<title>.*</title>"

The first must be 200 and the HTML must be your app’s index.html. The second must stay 404: if a missing script comes back 200 with HTML, the rule is shadowing files and the browser will show a blank page instead of a clear error.

On Livops

  • Usually nothing to do. If an app with a client-side router (React Router, Vue Router, etc.) is detected, page requests without a file extension get index.html, and a missing image or script still returns 404. That is the same split as nginx’s try_files: routes go to the app, broken asset links stay errors. See Redirects and headers.

  • A real 404 page. If the build output has a 404.html, it is served with a real 404 status.

  • When you need rules, the files work as you know them from Netlify and Vercel. _redirects sits at the root of the build output (public/_redirects becomes dist/_redirects). Each line is from to [status]; 200 is a rewrite within the site; :name matches one path segment and a trailing * matches the rest; the first matching rule wins:

    /users/*     /index.html   200
    /settings    /index.html   200
  • vercel.json and netlify.toml are read too. In vercel.json, rewrites become 200 rules, and a trailing :name*, :name+ or (.*) is treated as *. In netlify.toml, [[redirects]] with status = 200 work the same; a rule with conditions is skipped. Both files may sit at the project root, and for a site that is built they are carried into the release automatically. They are checked after _redirects: first vercel.json, then netlify.toml.

  • Be careful with a site-wide /* rule. Netlify’s and Vercel’s docs say an existing file wins over a rewrite. Livops’s docs promise no such thing; they say the first matching rule wins. So rather than /* /index.html 200, rely on the detection, or list the app’s routes as above, and look at any vercel.json or netlify.toml that came with the project from another host, since it is carried over automatically. Then run the curl check: a missing .js must still answer 404.

  • Changes are live with the deploy. The files are read on every request, so a new rule works as soon as the deploy finishes.

  • A blank page on a deep link means the page looks for its JS or CSS under another path. Check base in Vite: for a site at the root it is /. See No site found.

  • Which paths fail. The project’s Traffic tab lists the Top 404 paths, the ones most often answered “not found” (full analytics are on paid plans).

  • Free. A static site costs nothing on Livops; Vite, Create React App, Vue CLI, Angular and React Router are among the frameworks it detects.

If the build itself stops with Module not found: Can't resolve, see Module not found: Can’t resolve.

Questions

Why does the page work when I click a link but give 404 when I refresh?

A click is handled by the router inside the page, so the browser never asks the server for /about. A refresh, a bookmark or a shared link does ask the server, and a static host only has index.html and the asset files. Unless it is told to answer every route with index.html, it says 404.

Why does it work with npm run dev but not after deploy?

Vite treats an app as a single-page app by default and its development server falls back to index.html for unknown paths, and vite preview does the same. The static host that serves the built folder does not, until you add a rewrite or the host detects the app as a single-page app.

Is a rewrite to index.html the same as a redirect?

No. A rewrite (status 200) serves index.html while the address bar keeps /about, so the router sees the right path. A redirect (301 or 302) sends the browser to another address, and the router would see / instead of the page you asked for.

Should I use hash routing instead?

Only when you cannot configure the host. Hash URLs like /#/about need no server rule because the part after # is not sent to the server, but the addresses are uglier and Vue Router's docs say hash mode has a bad impact on SEO.

After the fix, a typo in the URL shows my app instead of a 404. Is that a bug?

It is the cost of the rewrite: every unknown path now gets index.html. Add a catch-all route at the end of your router that renders a Not found page, so visitors still see that the page does not exist.

The page opens blank on a deep link like /users/42. Is that the same problem?

Related but different. index.html now loads, but it asks for its scripts with a relative path such as assets/app.js, which the browser resolves under /users/. Build with a base of / so asset paths start from the root.

Do I need a rewrite rule on Livops?

Usually not. When Livops detects an app with a client-side router such as React Router or Vue Router, page requests without a file extension get index.html, and a missing image or script still returns 404. Rules in _redirects, vercel.json or netlify.toml are there for the cases detection does not cover.

Sources

  1. Netlify Docs — Rewrites and proxies (docs.netlify.com). Checked .
  2. Vercel Docs — Static Configuration with vercel.json (vercel.com). Checked .
  3. Vercel Docs — Vite on Vercel (vercel.com). Checked .
  4. nginx — Module ngx_http_core_module (try_files) (nginx.org). Checked .
  5. Vue Router — Different History modes (router.vuejs.org). Checked .
  6. React Router — Single Page App (SPA) (reactrouter.com). Checked .
  7. React Router — createBrowserRouter (reactrouter.com). Checked .
  8. React Router — createHashRouter (reactrouter.com). Checked .
  9. React Router — HashRouter (reactrouter.com). Checked .
  10. React Router — Routing (data mode) (reactrouter.com). Checked .
  11. Vite — Shared Options (appType, publicDir) (vite.dev). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your site for free

Got a question? — @livops_uz