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://donatexpress.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://donatexpress.vercel.app/Create), I get a 404: NOT_FOUND error.
App Details:
- Built using Vite with React.
- Using React Router for client-side routing with
<BrowserRouter>. - Routes in
App.jsxare defined like this:
<BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/Create" element={<CreateCampaign />} /> <Route path="/Donate" element={<Campaigns />} /> <Route path="/User" element={<UserCampaigns />} /> <Route path="/View" element={<ViewCampaign />} /> </Routes></BrowserRouter>What I Tried:
- Added a
vercel.jsonfile with the following content:
{ "routes": [ { "src": "/(.*)", "dest": "/index.html" } ]}-
Verified the
vite.config.jsfile and ensured the build output is correct. -
Previewed the app locally after running
npm run buildandnpm run preview, and it works fine.
Expected Behavior:
Routes like /Create or /Donate 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!