Why is my vercel project giving me an error 404 on refresh? [duplicate]
Asked Answered
G

3

8

I have a simple React + Vite project that I deployed on Vercel for the first time for fun. But whenever i hit refresh on a sub-route, it sends me to a 404 page.

I'm using react-router-v6 for the routing. When i hit refresh on the landing page, no problem. But when i navigate from the landing page to a another route and hit refresh, it gives me the 440 message for some reason.

I have no idea on how to fix this. Any help would be appreciated, thank you!

I did try adding a vercel.json file to my root directory but i have no idea how to configure it.. this is currently

{
  "routes": [
    { "src": "/chat", "dest": "/Chat" },
    { "src": "/[^.]+", "dest": "/", "status": 200 }
  ]
}

this is my App.tsx file

import "./App.scss";
import Login from "./pages/Login/login";
import Chat from "./pages/Chat/chat";
import { Routes, Route } from "react-router-dom";

function App() {
  return (
    <div className="App">
      <Routes>
        <Route path="/" element={<Login />} />
        <Route path="chat" element={<Chat />} />
      </Routes>
    </div>
  );
}

export default App;
Geffner answered 8/4, 2023 at 7:2 Comment(1)
Does this help answer your question? #64815512Leatherworker
P
22

Create a file called vercel.json on the root folder of your project with the following content and redeploy;

{ "routes": [{ "src": "/[^.]+", "dest": "/", "status": 200 }] }
Pieeyed answered 24/5, 2023 at 18:43 Comment(0)
A
4

Ensure that your server is configured to redirect all requests to your index.html file. For Vercel, create a vercel.json file with the following configuration:

{
  "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}
Anzovin answered 28/1, 2024 at 18:39 Comment(0)
D
1

// write this in a vercel.json file in the root directory

{ "rewrites": [ { "source": "/(.*)", "destination": "/index.html" } ] }
Dudden answered 2/9, 2024 at 10:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.