reactjsreact-router-domgithub-pages

React Router not working with Github Pages


My previous website only shows the home page when the home tab is clicked, then if you click my navbar brand, it says 404. This website worked on a create-react-app with npm start, but it doesn't work here, nor on the build. I don't know what is wrong with the app, maybe the router setup is messed up, I don't know. I have linked the App and Index pages where I have the router setup. If you need any more information, just ask me for more information.

Thank You

Index

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom'
import App from './App';
import './styles/index.css';

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

App

import { Routes, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import About from "./routes/About";
import Contact from "./routes/Contact";
import Home from "./routes/Home";
import Project from "./routes/Project";

const App = () => {
  return (
    <>
      <Navbar />
      <Routes>
        <Route path="/" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="/project" element={<Project />}></Route>
        <Route path="/contact" element={<Contact />}></Route>
      </Routes>
    </>
  );
};

export default App;

Solution

    1. If deploying to GitHub, ensure there is a "homepage" entry in package.json for where you are hosting it in Github.

      Examples:

      User Page

      "homepage": "https://amodhakal.github.io",
      

      Project Page

      "homepage": "https://amodhakal.github.io/portfolio",
      

      Custom Domain Page

      "homepage": "https://amodhakal.com",
      
    2. Switch to the HashRouter since GitHub pages doesn't support the tech used by the BrowserRouter.

      index

      import React from 'react';
      import ReactDOM from 'react-dom/client';
      import { HashRouter } from 'react-router-dom';
      import App from './App';
      import './styles/index.css';
      
      ReactDOM.createRoot(document.getElementById('root')).render(
        <React.StrictMode>
          <HashRouter>
            <App />
          </HashRouter>
        </React.StrictMode>
      );
      

      react-router@6.4+ data routers

      import React from 'react';
      import ReactDOM from 'react-dom/client';
      import {
        createHashRouter,
        RouterProvider
      } from 'react-router-dom';
      import App from './App';
      import './styles/index.css';
      
      const router = createHashRouter([
        {
          path: "/*",
          element: <App />,
        }
      ]);
      
      ReactDOM.createRoot(document.getElementById('root')).render(
        <React.StrictMode>
          <RouterProvider router={router} />
        </React.StrictMode>
      );
      

    For more details see the create-react-app docs for deploying to GitHub Pages and notes on client-side routing.