I’m deploying a React application built with Vite and using React Router for client-side routing. While the app works perfectly in development, I’m encountering issues when trying to access routes directly after deployment on Vercel. Here’s a summary of the problem:
Problem Description:
-
The app’s root URL (
https://manixh.vercel.app/) works fine and I can navigate between pages using the links. -
However, when I try to access a route directly (e.g., https://manixh.vercel.app/projects), I get a 404: NOT_FOUND error.
App Details:
Built using Vite with React.
Using React Router for client-side routing with .
Routes in App.jsx are defined like this:
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/projects" element={<Projects/>} />
<Route path="/blogs" element={<Blogs/>} />
</Routes>
</BrowserRouter>
What I Tried:
- Added a
vercel.jsonfile with the following content:
{
"rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}
- Verified the vite.config.js file and ensured the build output is correct.
- Previewed the app locally after running npm run build and npm run preview, and it works fine.
Expected Behavior:
Routes like /projects or /blogs should be handled by React Router and render the respective components.
Actual Behavior:
Directly accessing any route other than / results in a 404: NOT_FOUND error.
Could anyone guide me on the correct way to configure Vercel for a Vite app with React Router? Any help would be greatly appreciated!
