I have a React app (with vite and typescript) that works fine when using npm run dev. The main page also shows up correctly on github pages.
But it seems as soon as I try to redirect to the Products page or contact page it gives me a 404. I'm a beginner in terms of React and this really confuses me (especially because it does work perfect in run dev mode).
App.tsx
import { BrowserRouter, Route, Routes } from "react-router-dom";
return (
<div>
<Header />
<Cart cartItems={cartItems} setCartItems={setCartItems} />
<Routes>
<Route path="/shopping-cart" element={<Main />} />
<Route
path="shopping-cart/products"
element={<Products setCartItems={setCartItems} />}
/>
<Route path="shopping-cart/contact" element={<Contact />} />
</Routes>
<Footer />
</div>
);
}
main.tsx:
import { BrowserRouter } from "react-router-dom";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
vite config:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
base: '/shopping-cart/',
plugins: [react()],
})
(Full code https://github.com/Fibox-coder/shopping-cart)
I tried a lot of things like using Switch instead of Routes and use HashRouter instead of BrowserRouter but nothing seems to work.
I have 3 URL's on the Github Pages when clicking the navbar:
Works: Home = https://fibox-coder.github.io/shopping-cart/
404 File not found: Products = https://fibox-coder.github.io/shopping-cart/products/ Contact = https://fibox-coder.github.io/shopping-cart/contact