React 404 Errors After Deployment
React Router handles routing client-side, but when users refresh or directly access a URL, the server needs to return index.html for all paths. Without this, you get 404 errors.
Netlify
# netlify.toml
[[redirects]]
from = "/*"
to = "/index.html"
status = 200Or create public/_redirects:
/* /index.html 200Vercel
{
"rewrites": [
{ "source": "/(.*)", "destination": "/index.html" }
]
}Nginx
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri /index.html;
}
}homepage in package.json
If deployed to a subdirectory:
{
"homepage": "https://example.com/myapp"
}And set the basename in React Router:
<BrowserRouter basename="/myapp">
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>HashRouter as Alternative
If you can't configure the server, use HashRouter instead of BrowserRouter:
import { HashRouter } from 'react-router-dom';
// URLs become /#/about instead of /aboutGitHub Pages
{
"homepage": "https://username.github.io/repo-name",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
}Bugsly monitors error rates after deployments, catching 404 spikes immediately so you can fix the hosting configuration before users are impacted.
Try Bugsly Free
AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.
Get Started FreeRelated Articles
How to Fix Writablestream Error in Angular
Learn how to diagnose and fix Writablestream Error errors in Angular. Step-by-step guide with code examples.
Read moreFix Missing Import in Electron
Resolve module import errors in Electron apps caused by main/renderer process confusion, Node.js integration, and preload script issues.
Read moreFix Template Error in C#
Step-by-step guide to fix Template Error in C#. Includes root cause analysis, code examples, debugging tips, and prevention strategies.
Read moreHow to Fix Permissionerror in Django In Production
Learn how to diagnose and fix the permissionerror in Django in production. Includes code examples and prevention tips.
Read more