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",
    

    Vite: add the project directory as the base.

    vite.config.js

    export default {
      ...
      base: "/portfolio"
    };
    
  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'; // Note 1
    import App from './App';
    import './styles/index.css';
    
    ReactDOM.createRoot(document.getElementById('root')).render(
      <React.StrictMode>
        <HashRouter>
          <App />
        </HashRouter>
      </React.StrictMode>
    );
    

    react-router data routers

    import ReactDOM from 'react-dom/client';
    import App from './App';
    
    ReactDOM.createRoot(document.getElementById('root')).render(
      <React.StrictMode>
        <App />
      </React.StrictMode>
    );
    
    import {
      createHashRouter,
      RouterProvider
    } from 'react-router-dom'; // Note 1
    // import routed components
    
    const router = createHashRouter([
      ... routes configuration
    ]);
    
    const App = () => {
      ...
    
      return <RouterProvider router={router} />;
    };
    
    export default App;
    

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

1If using React-Router 7 library, all exports are from react-router instead of react-router-dom. Follow the RR7 installation and setup.

Answer from Drew Reese on Stack Overflow
Top answer
1 of 10
72
  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",
    

    Vite: add the project directory as the base.

    vite.config.js

    export default {
      ...
      base: "/portfolio"
    };
    
  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'; // Note 1
    import App from './App';
    import './styles/index.css';
    
    ReactDOM.createRoot(document.getElementById('root')).render(
      <React.StrictMode>
        <HashRouter>
          <App />
        </HashRouter>
      </React.StrictMode>
    );
    

    react-router data routers

    import ReactDOM from 'react-dom/client';
    import App from './App';
    
    ReactDOM.createRoot(document.getElementById('root')).render(
      <React.StrictMode>
        <App />
      </React.StrictMode>
    );
    
    import {
      createHashRouter,
      RouterProvider
    } from 'react-router-dom'; // Note 1
    // import routed components
    
    const router = createHashRouter([
      ... routes configuration
    ]);
    
    const App = () => {
      ...
    
      return <RouterProvider router={router} />;
    };
    
    export default App;
    

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

1If using React-Router 7 library, all exports are from react-router instead of react-router-dom. Follow the RR7 installation and setup.

2 of 10
17

I faced this similar routing problem in ReactJs when I used gh-pages.

My Problem: Routes are working fine at my local system but when I deployed my web app using gh-pages, I wasn't able to go to the sub-pages directly.

Example: ayushjaink8.github.io/cfhelper was working, and I am able to go to other pages from within the web app. But when I enter ayushjaink8.github.io/cfhelper/dashboard in the URL, it shows the github 404 error page.

Solution: I resolved the above problem by using <HashRouter/> and adding the homepage tag in the package.json like homepage: "/<repository-name>/#".

Note that gh-pages also follows the # rule in its URL routing. So it won't show any 404 error page if you write ayushjaink8.github.io/cfhelper/#/<any-route-of-your-app>.

Everything else remains the same in my codebase. I did not use the useHistory(), <BrowserRouter /> or any other functions. Only using <HashRouter/> works most of the time.

Your homepage becomes /<repo-name>/# instead of /<repo-name>. That's the only thing that change when you use <HashRouter/>.

For example, previously the homepage is: https://ayushjaink8.github.io/cfhelper/ with <HashRouter/>, the URL now becomes: https://ayushjaink8.github.io/cfhelper/#

This also applies to the routes.

Here goes my sample code:

import { Routes, Route, HashRouter } from "react-router-dom";

<HashRouter>
      <Navbar />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/auth/login" element={<Login />} />
        <Route path="/auth/signup" element={<Signup />} />
        <Route path="/dashboard" element={<Dashboard />} />
        <Route path="/contests/create" element={<CreateContest />} />
        <Route exact path="/contests/join" element={<JoinContest />} />
        <Route path="/contests/live" element={<LiveContest />} />
        <Route path="/practice" element={ <Practice /> } />
        <Route path="/analyze" element={ <Analyze /> } />
        <Route path="/verify" element={<VerifyEmail />} />
        <Route path="/profile/edit" element={<EditProfile />} />
      </Routes>
      <Footer />
</HashRouter>

Package.json Sample Code:

{
  "name": "cfhelper",
  "homepage": "/<your-github-repo-name>/#",
  "version": "1.0.0",
  "private": true,
}

🌐
GitHub
github.com › rafgraph › react-github-pages
GitHub - rafgraph/react-github-pages: React with React Router boilerplate for GitHub Pages
React for GitHub Pages is a lightweight solution for deploying React single page apps with React Router browserHistory using GitHub Pages.
Starred by 49 users
Forked by 11 users
Languages   JavaScript 82.4% | HTML 17.6% | JavaScript 82.4% | HTML 17.6%
Discussions

BrowserRouter is not compatible within a GitHub Pages deployment
To host the app on GitHub Pages, you need to use the BrowserRouter from the react-router-dom package in the project's root file. More on github.com
🌐 github.com
4
12
React Router and GitHub Page
Add a 404.html with this as the content https://github.com/rafgraph/spa-github-pages/blob/gh-pages/404.html More on reddit.com
🌐 r/react
2
2
April 5, 2023
reactjs - React Router v4: Cant load page on GitHub pages - Stack Overflow
Locally I can browse to /buttons just fine I can even refresh it and its fine too, but when I upload the build directory to github pages I can't access /buttons and instead I get the GitHub 404 pag... More on stackoverflow.com
🌐 stackoverflow.com
November 4, 2019
Can I create routes with react-router for a github-pages site?
Ok, so I've made a SPA using React and React-Router and have pushed it to github pages, but none of the routes I have written work when I refresh or click back in my browser. I had the same problem... More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
freecodecamp.org › news › deploy-a-react-app-to-github-pages
How to Deploy a Routed React App to GitHub Pages
February 22, 2021 - In our case, the route https://tomerpacific.github.io/starter-project/about doesn't help GitHub Pages understand where to point the user (since it is a frontend route). To overcome this problem, we need to use a Hash Router instead of a Browser Router in our application. This type of router uses the hash portion of the URL to keep the UI in sync with the URL. ReactDOM.render( <React.StrictMode> <HashRouter> <App /> </HashRouter> </React.StrictMode>, document.getElementById('root') );
🌐
Medium
medium.com › @Satyam_Mishra › react-router-deployment-to-gh-pages-issue-fixed-2024-bc7fd80946ad
React router deployment to gh-pages (issue fixed) | by Satyam Mishra | Medium
January 8, 2024 - You just have to create a base entry URL of your deployment link in your vite config file, it could be your GitHub link where the project will be deployed or any other deployment link/domain.
🌐
Medium
medium.com › swlh › using-react-router-on-github-pages-2702afdd5d0c
Using React Router on GitHub Pages | by Chris Liendo | The Startup | Medium
June 18, 2021 - import {BrowserRouter as Router} from 'react-router-dom' ... NOTE: in order for your app to work once deployed to GitHub Pages, the <Router> wrapping it will need to know its root URL.
🌐
Reddit
reddit.com › r/react › react router and github page
r/react on Reddit: React Router and GitHub Page
April 5, 2023 -

Hi Everyone, Would appreciate any help. I need help with fixing the routes in my React Portfolio. Has Github Page hosted it, and forward to my custom domain. There's something wrong between React Router and Github Page, only the Homepage is working right now, any other route just gives me 404 error. I found a similar issue on stackoverflow, followed it but no result.

StackoverFlow: https://stackoverflow.com/questions/71984401/react-router-not-working-with-github-pages

GitHub: https://github.com/RyanTrian/RyanTrian.github.io

Live: https://minhptruong.dev

Find elsewhere
🌐
DEV Community
dev.to › janjibdev › problem-with-react-router-app-and-github-pages-lij
Problem with react-router app and Github Pages (Solved !) - DEV Community
August 13, 2021 - However, you will notice some problems when deploying the react-router app to the GitHub pages.
🌐
Medium
medium.com › @swarajgosavi20 › how-to-deploy-react-app-with-client-side-routing-on-github-pages-8a3fefe5b0d5
How to deploy React app with client-side routing on GitHub Pages? | by Swaraj Gosavi | Medium
July 19, 2023 - Routing is an important part of website which enhances the user experience. React Routers are used to make routes in react app. But Github pages doesn’t support normal BrowserRouting.
Top answer
1 of 1
19

As per deployment docs at https://create-react-app.dev/, for GH pages:

GitHub Pages doesn’t support routers that use the HTML5 pushState history API under the hood (for example, React Router using browserHistory). This is because when there is a fresh page load for a url like http://user.github.io/todomvc/todos/42, where /todos/42 is a frontend route, the GitHub Pages server returns 404 because it knows nothing of /todos/42.

If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:

  • You could switch from using HTML5 history API to routing with hashes. If you use React Router, you can switch to hashHistory for this effect, but the URL will be longer and more verbose (for example, http://user.github.io/todomvc/#/todos/42?_k=yknaj) Read more about different history implementations of react-router.

Change your routing from:


    <BrowserRouter basename={process.env.PUBLIC_URL}>
      <Route render = {({ location }) => (
         <Layout location = { location }>
             <Switch location = { location }>
                <Route exact path = '/' component = { Home } />
                <Route exact path = '/buttons/' component = { Buttons } />
                <Route component = { NotFound }/>
              </Switch>
           </Layout>
       )} />
    </BrowserRouter>

To:

import { HashRouter, Route, Link } from "react-router-dom";

...


    <HashRouter basename={process.env.PUBLIC_URL}>
      <Route render = {({ location }) => (
         <Layout location = { location }>
             <Switch location = { location }>
                <Route exact path = '/' component = { Home } />
                <Route exact path = '/buttons/' component = { Buttons } />
                <Route component = { NotFound }/>
              </Switch>
           </Layout>
       )} />
    </HashRouter>
...

  • Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your index.html page with a special redirect parameter. You would need to add a 404.html file with the redirection code to the build folder before deploying your project, and you’ll need to add code handling the redirect parameter to index.html. You can find a detailed explanation of this technique in this guide.
🌐
Beautiful Code
beautifulcode.blog › react-routing-on-github-pages.html
How use React (Browser) Router with GitHub Pages | Beautiful Code
January 31, 2024 - I deployed my React App using GitHub Actions, the full workflow can be found here. The source code of the full example application can be found in my GitHub at: github.com/bloomsei/example-react-router. Of course, I deployed the app on GitHub Pages too: bloomsei.github.io/example-react-router/
🌐
GitHub
github.com › orgs › community › discussions › 64096
GitHub Pages does not support routing for single page apps · community · Discussion #64096
Navigating by clicking on links ... from my testing, there are 2 valid workarounds: When using react-router, use HashRouter instead of BrowserRouter....
🌐
DEV Community
dev.to › caffiendkitten › react-router-with-github-pages-en3
React Router with GitHub Pages - DEV Community
March 16, 2021 - This will give you the ability to import the proper tags by placing "import { BrowserRouter as Router, Route, NavLink} from "react-router-dom";". There are a few more tags that can be added such as Switch, Link, useRouteMatch, and useParams, but I used Route and NavLink. (I'm sure there are a few more but I am not aware of them.) (1) Once you have the import statement with the tags you like you can implement the routes tags in your project. For my I placed them in my Header file that will be the navigation of my app like so. The issue is that when you use GH Pages that it has a specific address, your gh name + github.io.
🌐
ITNEXT
itnext.io › so-you-want-to-host-your-single-age-react-app-on-github-pages-a826ab01e48
So you want to host your Single Page React App on GitHub Pages? | by Brendan McIlhenny | ITNEXT
April 28, 2018 - This is because when there is a ... on GitHub Pages, here are a couple of solutions:* You could switch from using HTML5 history API to routing with hashes....
🌐
Educative
educative.io › answers › how-to-deploy-a-react-app-on-github-pages
How to deploy a React app on GitHub pages
If your app has routing, wrap your routes inside <HashRouter basename="/">. We need this step to prevent GitHub from redirecting your app to 404 in case you refresh the page. import React from "react"; import { BrowserRouter, Switch, Route, Link, HashRouter, } from "react-router-dom"; export default function App() { return ( <BrowserRouter> <HashRouter basename="/"> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> <Link to="/users">Users</Link> </li> </ul> </nav> <Switch> <Route path="/about"> <About /> </Route> <Route path="/users"> <Users /> </Route> <Route path="/"> <Home /> </Route> </Switch> </HashRouter> </BrowserRouter> ); } function Home() { return <h2>Home</h2>; } function About() { return <h2>About</h2>; } function Users() { return <h2>Users</h2>; }
🌐
Medium
medium.com › @bennirus › deploying-a-create-react-app-with-routing-to-github-pages-f386b6ce84c2
Deploying a create-react-app with routing to GitHub pages | by Benni Russell | Medium
March 5, 2019 - We have successfully deployed a create-react-app with routing to a live URL on GitHub Pages. By utilising react-router-dom’s HashRouter we don’t have to worry about getting a 404 error on page refresh.
🌐
Medium
medium.com › @arijit_chowdhury › deploy-react-app-with-react-router-to-github-pages-for-free-569377f483f
Deploy React App with React Router to Github Pages for Free | by Arijit Chowdhury | Medium
June 8, 2020 - This article is to show you how to deploy React application to Github-pages quickly. In this tutorial, I will also show you how to configure React Router to your app.
🌐
LogRocket
blog.logrocket.com › home › how to deploy react apps to github pages
How to deploy React apps to GitHub Pages - LogRocket Blog
April 22, 2025 - If we want to handle page routing when we deploy to GitHub Pages, we’ll need to do something similar. Let’s configure routing for our previously deployed project. First, we need to install a router. Start by installing React Router in the project directory, like so:
🌐
Medium
gmfuster.medium.com › deploying-a-react-app-to-github-pages-24c3e5485589
Deploying a React app to GitHub Pages
February 11, 2021 - When you are using HashRoute you don’t need to change the REACT_APP_FOR_PATH to change between building locally and building for GitHub pages. I just have it there anyway and have the code using the path on the links in case I switch back later. I was also having issues with my <Nav.Link>s because those were not using the routing with the hash symbol. What I did was replace those inside the nav with the React <Link> and give them my own styling. import {Link as ReactLink} from ‘react-router-dom’;