How to solve Github pages 404 problem when I use react router? - reactjs

I try to deploy my typescript react app to Github pages, but every time when I access my URL, Github complains '404 there isn't a github pages site here.'
And I try to make my code simple, I just left the home page in my app, but it still complains.
If I don't use React router, it works. Why does it complain when I use react router?
My Code:
import Home from "./pages/Home" //rafce
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
const Router: React.FC = () => {
return <>
<BrowserRouter>
<Routes>
<Route path="/" ><Home /></Route>
</Routes>
</BrowserRouter>
</>
}
export default Router
My JSON:
{
"homepage": "http://lsk21007.github.io/ohno",
"name": "typescript-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^6.1.1",
"#fortawesome/free-regular-svg-icons": "^6.1.1",
"#fortawesome/free-solid-svg-icons": "^6.1.1",
"#fortawesome/react-fontawesome": "^0.1.18",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.5.1",
"#types/node": "^16.11.36",
"#types/react": "^18.0.9",
"#types/react-dom": "^18.0.4",
"bootstrap": "^5.2.0-beta1",
"react": "^18.1.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.1.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"typescript": "^4.6.4",
"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": {
"#types/styled-components": "^5.1.25",
"gh-pages": "^4.0.0"
}
}
Thanks for your help!!

Related

React Router Url displays blank page after running npm serve

I have an issue with running npm serve for a static react SPA. I believe it has to do with the react-router-dom from react-router.
Below is the package json
{
"name": "eureka-customer-ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"#reduxjs/toolkit": "^1.7.1",
"#webstudio/buttons": "^5.3.2",
"#webstudio/checkbox": "^2.1.6",
"#webstudio/icons": "^5.0.4",
"#webstudio/inputs": "^4.11.0",
"#webstudio/theme-dbs": "^2.0.1",
"#webstudio/tooltip": "^2.3.1",
"#webstudio/typography": "^1.0.3",
"axios": "^0.24.0",
"formik": "^2.2.9",
"html-react-parser": "^1.4.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-redux": "^7.2.6",
"react-router-dom": "^6.2.1",
"react-scripts": "4.0.3",
"styled-components": "^5.3.3",
"web-vitals": "^2.1.3",
"yup": "^0.32.11"
},
"scripts": {
"clean": "rm -rf build && rm -rf node_modules",
"lint": "eslint './src/**/*.{js,ts,tsx}' --max-warnings 5",
"start": "react-scripts start ",
"build:clean": "rm -rf build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"setup": "npm ci",
"preinstall": "npx npm-force-resolutions",
"package": "cd build && zip -r ../$BUILD_ENV.zip *",
"copy:static": "cp Staticfile build",
"serve": "npx serve -s build -l 5000"
},
"eslintConfig": {
"extends": [
"react-app"
],
"rules": {
"import/no-anonymous-default-export": "off"
}
},
"eslintIgnore": [
"src/components/DBSDefault/*.tsx"
],
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^13.1.5",
"#types/jest": "^24.9.1",
"#types/node": "^16.11.19",
"#types/react": "^16.9.53",
"#types/react-dom": "^16.9.9",
"#types/react-helmet": "^6.1.5",
"#types/styled-components": "^5.1.19",
"#types/uuid": "^8.3.4",
"#typescript-eslint/eslint-plugin": "^4.17.0",
"#typescript-eslint/parser": "^4.17.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-cypress": "^2.11.2",
"eslint-plugin-jest": "^24.2.1",
"eslint-plugin-prettier": "^3.3.1",
"local-web-server": "^5.1.1",
"serve": "^13.0.2",
"typescript": "^4.4.4",
"ws": "^8.5.0"
}
}
After running npm run build and npm run serve, i am unable to hit a PATH defined in my routes such as localhost:5000/personal/cards/subscription/signup. I get a blank page instead.
Below is a snippet of my routes components
import ConfirmationPage from "pages/ConfirmationPage";
import SignUpForm from "pages/SignUpForm";
import React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
export default () => {
return (
<BrowserRouter>
<Routes>
<Route
path="/personal/cards/subscription/signup"
element={<SignUpForm />}
/>
<Route
path="/personal/cards/subscription/confirm"
element={<ConfirmationPage />}
/>
</Routes>
</BrowserRouter>
);
};
Any ideas? Thank you!
Solved it by replace BrowserRouter with HashRouter.

React-Bootstrap - CRA - Failed to compile

When I install react-bootstrap in my CRA react app, and try to import any component such as:
import Form from "react-bootstrap";
I get this error:
Failed to compile.
./node_modules/react-bootstrap/esm/SplitButton.js
Module parse failed: Unexpected token (101:2)
You may need an appropriate loader to handle this file type.
| renderMenuOnMount,
| rootCloseEvent,
| ...props
| }, ref) => /*#__PURE__*/_jsxs(Dropdown, {
| ref: ref,
My package.json looks like this:
{
"name": "component-3",
"version": "0.1.1",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.15.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"axios": "^0.23.0",
"bootstrap": "^5.1.3",
"dotenv": "^10.0.0",
"firebase": "^8.10.0",
"firebase-admin": "^9.12.0",
"react": "^17.0.2",
"react-bootstrap": "^2.0.2",
"react-color-extractor": "^1.1.2",
"react-dom": "^17.0.2",
"react-firebase-hooks": "^3.0.4",
"react-google-button": "^0.7.2",
"react-router-dom": "^5.2.0",
"react-scripts": "^1.1.5",
"react-select": "^5.2.1",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "set NODE_ENV=dev && CHOKIDAR_USEPOLLING=true 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 op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I want to use components such as Form in my app. I have used react-bootstrap with CRA before with no issues.
I have seen similar posts but none with this exact issue.
Any idea what might be causing this?

Deploying create-react-app --template-typescript to gh pages

I have a problem with deploying typescript react app, all I get is the most outer element from the MainTemplate.tsx file, I should be seeing a logging screen, there are no routes available for me that I have available locally. Also, I have done npm run build many times now, and it is saying that my last deploy was 14 hours ago, how can I refresh it? I tried to change my deply script to this one but did not work:
"deploy": "./node_modules/gh-pages/bin/gh-pages-clean.js && gh-pages -d build"
this is my package.json
{
"homepage": "https://dziekonskik.github.io/project-money-front",
"name": "project-money-front",
"version": "0.1.0",
"private": false,
"proxy": "http://localhost:3000",
"dependencies": {
"#aws-amplify/ui-react": "^1.2.3",
"#reduxjs/toolkit": "^1.5.1",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"#types/jest": "^24.9.1",
"#types/node": "^12.20.14",
"#types/react": "^16.14.8",
"#types/react-dom": "^16.9.13",
"#types/react-redux": "^7.1.16",
"#types/styled-components": "^5.1.9",
"aws-amplify": "^4.1.1",
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"formik": "^2.2.9",
"gsap": "^3.7.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.4",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"styled-components": "^5.3.0",
"styled-reset": "^4.3.4",
"typescript": "~4.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "./node_modules/gh-pages/bin/gh-pages-clean.js && gh-pages -d build"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/react-router-dom": "^5.1.7",
"gh-pages": "^3.2.3"
}
}
Thanks!
Thank you #yume_chan changing BrowserRouter component to HashRouter did the job. Also there was no need to modify deploy script when I changed back to
"deploy": "gh-pages -d build"
worked as well with HashRouter
Regards!

images are not displaying after adding homepage in package.json of react js app

in order to deploy react app on the tomcat server I added homepage in the react app package.json.but after that images disappears from the react page.for image storage i have new folder image folder in the public folder and saved all my images there..and using the path to fetch images '/images/image1.jpg'
here is package.json
{
"name": "reactapp",
"version": "0.1.0",
"private": true,
"homepage": "https://localhost:8080/bunge",
"dependencies": {
"#fortawesome/fontawesome-free": "^5.15.3",
"#testing-library/jest-dom": "^5.11.10",
"#testing-library/react": "^11.2.6",
"#testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"bootstrap": "^4.6.0",
"bootstrap-icons": "^1.4.1",
"font-awesome": "^4.7.0",
"react": "^17.0.2",
"react-bootstrap": "^1.5.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.3",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"react-thunk": "^1.0.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"web-vitals": "^1.1.1"
},
"scripts": {
"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"
]
}
}

Heroku ReactJS app renders blank page but works fine locally

I deployed a ReactJS + ExpressJS app on heroku and it renders the index.html but without the React App in it. This is the package.json:
{
"name": "spotifyproj",
"version": "0.1.0",
"private": true,
"homepage": "https://whispering-caverns-57172.herokuapp.com",
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"bootstrap": "^4.5.2",
"compression": "^1.7.4",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"morgan": "^1.10.0",
"path": "^0.12.7",
"querystring": "^0.2.0",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-spotify-player": "^1.0.4",
"spotify-web-api-js": "^1.5.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "npm run build"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
It works fine locally but wont render the app when its deployed...
Solved it in server.js
You gotta tell express to use the build path and not the src or public directory
app.use(express.static(path.join(__dirname, 'build')))
.use(cors())
.use(cookieParser());

Resources