2

I've previously created and deployed single-page react apps on GitHub and netlify and they all worked fine. the problem is with multi-page apps using react-router. to test this I tried several times with different apps and as soon as I implement react-router and link to different pages, they become blank after deployment. here's a test app:

index.js

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

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

App.js

import React from "react";
import { Routes, Route } from "react-router-dom";
import Index from "./pages/Index";
import About from "./pages/About";
import Products from "./pages/Products";
import Navbar from "./components/Navbar";

const App = () => {
return (
<>
  <Navbar />
  <Routes>
    <Route exact path="/" element={<Index />} />
    <Route path="/about" element={<About />} />
    <Route path="/products" element={<Products />} />
  </Routes>
</>
 );
}; 

export default App;

Navbar.js component

import React from "react";
import {Link} from "react-router-dom";

const Navbar = () => {
 return (
   <>
   <nav className="navbar">
    <ul>
      <li>
        <Link to="/">home</a>
      </li>
      <li>
        <Link to="/about">about</a>
      </li>
      <li>
        <Link to="/products">products</a>
      </li>
    </ul>
  </nav>
</>
);
};

export default Navbar;

Package.json

{
 "name": "test-app",
 "version": "0.1.0",
 "homepage": "https://EricSsSnake/github.io/test-app",
 "private": true,
  "dependencies": {
  "@testing-library/jest-dom": "^5.16.5",
  "@testing-library/react": "^13.4.0",
  "@testing-library/user-event": "^13.5.0",
  "react": "^18.2.0",
  "react-dom": "^18.2.0",
  "react-router-dom": "^6.4.0",
  "react-scripts": "5.0.1",
  "web-vitals": "^2.1.4"
 },
  "scripts": {
   "predeploy": "npm run build",
   "deploy": "gh-pages -d build",
   "start": "react-scripts start",
   "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": "^4.0.0"
 }
}

plus 3 pages to navigate to.

When I deploy this on GitHub all I get is a blank page with these errors. screenshot

for deployment, I follow this tutorial: react-gh-pages

I'd really appreciate your help guys. this issue has stopped me from making any progress. I spent 3 weeks making my portfolio and now it's just sitting there.

update: replacing BrowserRouter with HashRouter fixed the initial problem and there are no longer any errors in the console. however, what I'm getting now is a blank page, as if the route that the index page is set to doesn't exist. I think the problem I have now is similar to this thread: Getting a blank page after deploying reactjs app to github pages

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • 2
    Try using `HashRouter` instead of `BrowserRouter`. I know it won't fix it but then we can rule out the possibility of JS not being able to handle routes other than root. Or similar issues. – TheWhiteFang Sep 16 '22 at 05:10
  • Commenting because im not sure if this works! but try adding a basename. ``. if applying this fixed your problem, you need to revise your server configurations. – punjira Sep 16 '22 at 05:40

2 Answers2

2

change :

"homepage": "https://EricSsSnake/github.io/test-app",

to :

"homepage": "https://EricSsSnake.github.io/test-app",

and use HashRouter instead of BrowserRouter . in your case change your index.js like this :

import React from "react";
import ReactDOM from "react-dom/client";
import { HashRouter } from "react-router-dom";
import "./index.css";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
 root.render(
  <React.StrictMode>
    <HashRouter>
     <App />
    </HashRouter>
  </React.StrictMode>
  );

Note : githup pages do not support React Router with browserHistory because it's using HTML5 history API that's why you can switch to to routing with hashes ( HashRouter in react router) check in the official doc of create-react-app

monim
  • 3,641
  • 2
  • 10
  • 25
  • let me know if it didn't work – monim Sep 16 '22 at 06:21
  • Thanks your solution solved the problem but another one arose :( There are no longer any errors in the console but now what I get is a blank page. my navbar is rendered but when I click on any link it shows a 404 message. so it doesn't fully render the index page and cannot find any of the other pages. – Eric SsSnake Sep 16 '22 at 13:13
  • I updated the question and explained the new problem at the end. would you kindly take a look – Eric SsSnake Sep 16 '22 at 13:35
  • In the **navbar** component change `a` tag to `Link` like this `home` and `about` and `products` don't forget to `import Link from 'react-router-dom'` – monim Sep 16 '22 at 14:50
  • I'll update my answer if it worked – monim Sep 16 '22 at 14:52
  • Thanks so much for your responses. didn't work. – Eric SsSnake Sep 17 '22 at 04:27
  • please update the navbar in your question to check if everything is okey ! and just get rid of **StrictMode** in your index.js – monim Sep 17 '22 at 12:04
0

Is there any error in the console?

If not, try using <Link /> component imported from 'react-router-dom' or 'react-router' instead of '' provided by html, because <a> will trigger a page refresh and <Link /> won't.

hope this answer help you