Search code examples
javascriptreactjsgithubreact-router-domgithub-pages

React HashRouter doesn't render anything on GitHub Pages


HashRouter doesn't render anything on GitHub pages and the console doesn't show any errors either. Also it renders locally only if I put a # before the app name so instead of typing: localhost:3000/chat-app I have to type localhost:3000/#/chat-app.

App.js

import SignIn from './Components/SignIn';
import SignUp from './Components/SignUp';
import Home from './Components/Home';
import UserProfile from './Components/UserProfile';
import { HashRouter, Routes, Route } from 'react-router-dom'

function App() {
  return (
    <HashRouter>
      <Routes>
        <Route path="/chat-app" exact element={<Home />} />
        <Route path="/chat-app/signIn" exact element={<SignIn />} />
        <Route path="/chat-app/signUp" exact element={<SignUp />} />
        <Route path="/chat-app/userProfile" exact element={<UserProfile />} />
      </Routes>
    </HashRouter>
  );
}

export default App;

package.json:

{
  "homepage": "https://melosshabi.github.io/chat-app",
  "name": "chat-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "firebase": "^9.16.0",
    "nanoid": "^4.0.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.8.0",
    "react-scripts": "5.0.1",
    "universal-cookie": "^4.0.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "gh-pages": "^5.0.0"
  }
}

I have tried some fixes I found online but none of them work. If you need the repo link here it is: https://github.com/melosshabi/chat-app

I have tried changing the import from: import { HashRouter as Router, Routes, Route } from 'react-router-dom' to: import { HashRouter, Routes, Route } from 'react-router-dom'

I have tried changing the app name.

I also tried changing :<Route path="/chat-app/home" element={<Home />} /> to: <Route path="/chat-app" exact element={<Home />} />


Solution

  • Remove "/chat-app" from all the routes since including this would make the absolute URLs https://melosshabi.github.io/chat-app/#/chat-app and https://melosshabi.github.io/chat-app/#/chat-app/signin, etc.

    enter image description here

    If necessary you can specify a basename prop on the router. I don't think it will be required in your case.

    There is also no exact prop on the Route component; in RRDv6 all routes are always exactly matched.

    function App() {
      return (
        <HashRouter basename="/chat-app"> // <-- if necessary
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/signIn" element={<SignIn />} />
            <Route path="/signUp" element={<SignUp />} />
            <Route path="/userProfile" element={<UserProfile />} />
          </Routes>
        </HashRouter>
      );
    }