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 › orgs › community › discussions › 64096
GitHub Pages does not support routing for single page apps · community · Discussion #64096
Currently, GitHub Pages doesn't natively support SPA routing. This is indeed a limitation that many developers have raised. For now, the common workarounds you mentioned are: Using HashRouter in react-router to have URLs like /#/page1.
Discussions

reactjs - React Router v4: Cant load page on GitHub pages - Stack Overflow
Yes if I remove it then the default route / shows the NotFound route and /buttons is still not accessible ... GitHub Pages doesn’t support routers that use the HTML5 pushState history API under the hood (for example, React Router using browserHistory). More on stackoverflow.com
🌐 stackoverflow.com
November 4, 2019
Gh-pages deployment problems with react-router
Added react-router and just a Home page and Page404 to the router. When I navigate to the declared homepage: https://rockchalkwushock.github.io/rcws-development/ I am first being directed to my Pag... More on github.com
🌐 github.com
10
March 8, 2017
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
BrowserRouter is not compatible within a GitHub Pages deployment
Hi, BrowserRouter is not compatible out-of-the-box within a GitHub Pages deployment and is expected to result in 404s - so using HashRouter is the best alternative. The problem is that with HashRou... More on github.com
🌐 github.com
4
12
🌐
GitHub
github.com › orgs › community › discussions › 58502
Help, React router dom problem · community · Discussion #58502
Ensure that your React Router configuration uses BrowserRouter instead of HashRouter. This is important because GitHub Pages does not support the use of the hash fragment (#) in URLs.
🌐
DEV Community
dev.to › azadulkabir455 › react-router-not-working-after-react-app-published-through-github-actions-1ddj
React Router not working after "React app" published through GitHub actions - DEV Community
May 14, 2024 - If your React application is not working properly after being published on GitHub Pages, it's often related to the routing configuration. Here are a few common issues and their solutions: GitHub Pages serves the index.html for all 404 responses by default, which means when using React Router, you need to ensure all routes fall back to index.html.
🌐
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 - Maybe you should recheck your codes that involve react router v6 ... Thanks for the post. It helped me figure out the links for my app on gh-pages and I got it to work ... Hi , I think i made all possible changes but still my react routing application isn't working on github, Could anyone please help me to solve this issue?
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.
🌐
GitHub
github.com › facebook › create-react-app › issues › 1765
Gh-pages deployment problems with react-router · Issue #1765 · facebook/create-react-app
March 8, 2017 - export default () => ( <Router history={browserHistory} onUpdate={logPageView}> <Route path='/' component={App}> <IndexRoute component={Home} /> </Route> <Route path='*' component={Page404} /> </Router> ); Seeing that my homepage is pointing to /rcws-development/ I thought perhaps this was the issue; because when I use the redirect button on the Page404 I'm sent to https://rockchalkwushock.github.io/. So I changed my homepage: https://rockchalkwushock.github.io/ but no such luck. I then get Github's 404. I followed the instructions from the README and went to the following repository after reading that gh-pages does not natively support front-end routing.
Author   rockchalkwushock
Find elsewhere
🌐
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
gmfuster.medium.com › deploying-a-react-app-to-github-pages-24c3e5485589
How I finally got my React app with Routing to work on GitHub ...
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’;
🌐
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

🌐
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 - After what feels like a century of banging my head against the hardest and roughest of brick walls I was finally able to get my frontend React app hosted on GitHub Pages, or so I thought. As how most things work out in life, it was not until I fixed the issue with little direction that I saw that as part of the files that comes baked into a React app is the README.md which reads: #### Notes on client-side routingGitHub Pages doesn’t support routers that use the HTML5 `pushState` history API under the hood (for example, React Router using `browserHistory`).
🌐
Medium
medium.com › @Dragonza › react-router-problem-with-gh-pages-c93a5e243819
React-router problem with gh-pages | by Ngoc Vuong | Medium
August 27, 2018 - Recently, I has been working on my personal project created with React, React-router and Redux. Everything worked pretty neat on my local. However, when I tried to deploy it on github project page, I encountered a very prevalent problem. It seemed that the routing did not recognize the path, it always returned 404 page.
🌐
Medium
medium.com › @faithnjah5 › react-router-on-github-pages-fix-deployment-issues-in-6-simple-steps-ec8c1b358e76
React Router on GitHub Pages: Fix Deployment Issues in 6 Simple Steps | by Faith Njah | Medium
October 5, 2025 - If your app uses React Router, you might encounter issues with routing on GitHub Pages. Here’s how to fix it: ... Then follow the deployment process outlined above. When using HashRouter, note that your URLs will include a # symbol (e.g., yoursite.github.io/#/about).
🌐
DEV Community
dev.to › janjibdev › problem-with-react-router-app-and-github-pages-lij › comments
[Discussion] Problem with react-router app and Github Pages (Solved !) — DEV Community
Also, going back to the home page loads the error page. ... Computer engineering student and a tech enthusiast. ... I just simply wrapped all my routes inside the HashRouter instead of BrowserRouter and it works fine ... Yeah, your method is the simplest one actually. However, it doesn't work with my program so that is why I figure out other ways ... I just tried this method by using react router v6 and its still working.
🌐
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 - NOTE: if you plan on deploying to GitHub Pages, the path of your ‘root’ route (here on line 33) must be ''. If set to '/' it will work locally but not once deployed. ... We also need to create links that point to each Route’s path. Start by importing Link from react-router-dom.
🌐
Devavatar
devavatar.com › posts › fix-react-router-on-github-pages
Fixing broken Client-Side React Router on GitHub Pages Deployment | Dev Avatar
April 9, 2024 - GitHub Pages doesn’t support client-side routing out of the box unless all routes are children of /. ... Make all routes children of /, (Note: This may not solve the problem properly, and may still result in 404 if the user refreshes the page ...
🌐
DEV Community
dev.to › caffiendkitten › react-router-with-github-pages-en3
React Router with GitHub Pages - DEV Community
March 16, 2021 - 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.
🌐
GitHub
github.com › orgs › community › discussions › 55735
React app with React Router deployed to Github Pages Unable to access a particular Route · community · Discussion #55735
This may work for SPA, but in my case it didn't. I have the solution, you convert Vite React App to Create React App and you upload to Netlify by following this tutorial: https://www.freecodecamp.org/news/how-to-deploy-react-router-based-app-to-netlify/ How to deploy a Vite React App to Github Pages, you deploy a Create React App to Netlify. Beta Was this translation helpful? Give feedback. ... Sign up for free to join this conversation on ...