DEV Community

Cover image for React app returns 404 on refresh? Here's the 2-minute fix
YOUNARES
YOUNARES

Posted on

React app returns 404 on refresh? Here's the 2-minute fix

You deploy your React app, click around, and everything works. Then you refresh on /about and get a 404.

Here's why it happens, and how to fix it on the most common hosts.

Why it happens

In a single-page app, there's only one real file: index.html. React Router changes the page in the browser, so /about doesn't exist on the server. When you refresh, the server looks for an about file, finds nothing, and returns 404.

The fix is to tell your host: for any route you don't recognize, serve index.html.

Vercel

Create a vercel.json in the root of your project:

{
  "rewrites": [{ "source": "/(.*)", "destination": "/" }]
}
Enter fullscreen mode Exit fullscreen mode

Cloudflare Pages

Create a public/_redirects file:

/*  /index.html  200
Enter fullscreen mode Exit fullscreen mode

Netlify

Same idea, same file: add a public/_redirects file with the line above.

Check it worked

  1. Redeploy.
  2. Open your site, go to a deep route like /about.
  3. Refresh. It should load instead of showing a 404.

Still seeing a blank page?

The 404 might not be the real problem. A wrong base path, missing env variables and case-sensitive file names cause blank pages too. I covered all of them here: Vite app shows a blank page after deploying? Check these 6 things

And if you want the full checklist as a free PDF with a decision tree and a pre-deployment checklist: React Deployment Rescue Kit

Top comments (0)