DEV Community

Cover image for Fixing Routing Issues in Vite React App on Vercel
pwnkdm
pwnkdm

Posted on

3 1 1

Fixing Routing Issues in Vite React App on Vercel

Deploying a Vite React app on Vercel is straightforward, but many developers face a common issue: navigating to another page and refreshing results in a 404 error. This happens because Vercel serves only index.html and doesn't recognize client-side routing handled by React Router.

The Problem

When using React Router, routes are managed on the client-side. However, Vercel doesn’t know how to handle these routes after a refresh, leading to a 404 error.

The Solution: Configure vercel.json

To fix this, create a vercel.json file inside your frontend root directory (same level as index.html) and add the following:

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

Explanation of vercel.json

  • rewrites: Defines URL rewriting rules.
  • source: "/(.*)" captures all routes.
  • destination: Redirects all requests to index.html, allowing React Router to handle routing.

Deploy Again

After adding vercel.json, redeploy your Vite app using:

vercel --prod
Enter fullscreen mode Exit fullscreen mode

Now, navigating and refreshing pages will work without 404 errors. 🎉

This simple fix ensures your Vite React app works smoothly with React Router on Vercel. Happy coding! 🚀

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️