I have a react website published on github pages, my hompage and repository is
/websiteName
so it's working when I'm at the hompage, ex:
username.github.io/websiteName
. But I also have a route which is
/user/:id
, so when I click a link it will redirect me to
username.github.io/user/123
thus giving me a 404 error. So how do I fix the 404 error when redirecting to a different route other than
/websiteName
.
Can I create routes with react-router for a github-pages site?
BrowserRouter is not compatible within a GitHub Pages deployment
My page comes out 404 with routing
reactjs - React Router v4: Cant load page on GitHub pages - Stack Overflow
Videos
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" };Switch to the
HashRoutersince GitHub pages doesn't support the tech used by theBrowserRouter.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-routerdata routersimport 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.
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,
}
I think you need to change your browserHistory to a hashHistory.. so you can use it with gh... it changes path from /home to #/home
Aside from using hashHistory as suggested in the accepted answer, there is another workaround. Look here.
Basically, you create a spoof 404.html file which has a script that converts the requested path into the query string & redirects the browser to the index page with the query string attached to the URL. After the index file is loaded, the original path is restored from the query string & ReactRouter picks up the changes.
A neat solution, but not production-ready, either.