React Router Url displays blank page after running npm serve - reactjs

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.

Related

Subdomain localhost: Invalid host header

I currently have my website at localhost:3000 and i also created a subdomain for the admin panel at admin.localhost:3000. I configured my index.js to render 2 different apps depending on the window.location.host.
Everything was working accordingly then for some reason when i logged out of my admin panel and refreshed admin.localhost:3000 i got the INVALID HOST HEADER.
Here is my index.js file:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import AppAdmin from './AppAdmin';
import App from './App';
let host = window.location.host;
let protocol = window.location.protocol;
let parts = host.split(".");
if ((parts.length >= 2 && parts[0] !== "admin")) {
let subdomain = parts[0];
parts.splice(0, 1);
window.location = protocol + "//" + parts.join(".") + "/";
}
const parsedData = window.location.host.split(".");
console.log(parsedData)
if(parsedData[0]==='admin'){
ReactDOM.render(<AppAdmin />, document.getElementById('root'));
}else{
ReactDOM.render(<App />, document.getElementById('root'));
}
here is my package.json:
{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#ashvin27/react-datatable": "^1.5.3",
"#fortawesome/fontawesome-svg-core": "^1.2.19",
"#fortawesome/free-brands-svg-icons": "^5.9.0",
"#fortawesome/free-regular-svg-icons": "^5.9.0",
"#fortawesome/free-solid-svg-icons": "^5.9.0",
"#fortawesome/react-fontawesome": "^0.1.4",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.19.0",
"bootstrap": "^4.6.1",
"classnames": "^2.2.6",
"dotenv": "^16.0.1",
"font-awesome": "^4.7.0",
"http-proxy-middleware": "^2.0.6",
"is-empty": "^1.2.0",
"jquery": "^3.4.1",
"jwt-decode": "^2.2.0",
"popper.js": "^1.15.0",
"prop-types": "^15.7.2",
"ra-data-json-server": "^2.9.3",
"react": "^18.2.0",
"react-admin": "^4.1.3",
"react-app-rewired": "^2.2.1",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.2.0",
"react-redux": "^7.1.0",
"react-router-dom": "^5.0.1",
"react-scripts": "5.0.1",
"react-toastify": "^5.3.1",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "SET NODE_ENV=production && webpack-dev-server --host 127.0.0.1 --inline --history-api-fallback --content-base . ",
"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"
]
},
"proxy": "http://localhost:9000/"
}

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

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!!

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!

npm run build failing because of TypeScript error in docker build

Failed to compile.
/app/node_modules/#ant-design/icons/lib/components/AntdIcon.d.ts
TypeScript error in /app/node_modules/#ant-design/icons/lib/components/AntdIcon.d.ts(2,13):
'=' expected. TS1005
1 | import * as React from 'react';
> 2 | import type { IconDefinition } from '#ant-design/icons-svg/lib/types';
| ^
3 | import type { IconBaseProps } from './Icon';
4 | import { getTwoToneColor, TwoToneColor, setTwoToneColor } from './twoTonePrimaryColor';
5 | export interface AntdIconProps extends IconBaseProps {
my package.json
{
"name": "web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#types/react-native-fetch-blob": "^0.10.5",
"#types/react-select": "^3.0.16",
"#types/rn-fetch-blob": "^1.2.1",
"antd": "^4.2.4",
"common-tags": "^1.8.0",
"lodash": "^4.17.19",
"moment": "^2.26.0",
"node-sass": "^4.13.1",
"normalize.css": "^8.0.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"rn-fetch-blob": "^0.12.0",
"socket.io-client": "^2.3.0",
"typescript": "~3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 9000 -c .storybook"
},
"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": {
"#storybook/addon-actions": "^5.3.18",
"#storybook/addon-info": "^5.3.18",
"#storybook/addon-knobs": "^5.3.18",
"#storybook/addon-notes": "^5.3.18",
"#storybook/addons": "^5.3.18",
"#storybook/preset-create-react-app": "^2.1.2",
"#storybook/react": "^5.3.18",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"#types/common-tags": "^1.8.0",
"#types/jest": "^24.0.0",
"#types/lodash": "^4.14.150",
"#types/node": "^12.0.0",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"#types/react-redux": "^7.1.7",
"#types/react-router-dom": "^5.1.4",
"#types/redux": "^3.6.0",
"#types/socket.io-client": "^1.4.32",
"babel-loader": "8.1.0",
"react-docgen-typescript-loader": "^3.7.2"
}
}
My Dockerfile
FROM node:12 as builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
RUN npm run build
FROM nginx:stable
COPY --from=builder /app/build /usr/share/nginx/html
After googling about the issue came to know https://github.com/ant-design/ant-design/issues/23266#issuecomment-613773502 you need update #types/react .I tried "#types/react" : "17.0.1". But didn't help.
Antd v4.2.4 uses TypeScript v3.9.2.
Feature import type which compiler complains is supported since TypeScript v3.8.
This feature is something most users may never have to think about; however, if you’ve hit issues under --isolatedModules, TypeScript’s transpileModule API, or Babel, this feature might be relevant.
import type { SomeThing } from "./some-module.js";
export type { SomeThing };
source: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html
Update your local TypeScript version from v3.7.2 to atleast v3.8.

Why are the icons not showing up on my Ant Design sider?

<Sider collapsible collapsed={this.state.collapsed} onCollapse={this.onCollapse} breakpoint="md">
<Menu
theme="dark"
mode="inline"
defaultSelectedKeys={['10']}
style={{ lineHeight: '64px' }}
>
<Menu.Item key="2" icon={<LoginOutlined />}><Link to="/login">Login</Link></Menu.Item>
This is what my Sider code looks like. But still, the icon doesn't show on the Sider. This code works perfectly on CodeSandBox. Using the latest version of Chrome.
Even the collapsed form doesn't show icons.
Please help.
Edits:
1. CodeSandbox Link: https://codesandbox.io/s/sider-ant-design-demo-zkpcs?fontsize=14&hidenavigation=1&theme=dark
2. package.json:
{
"name": "gui",
"version": "0.1.0",
"private": true,
"homepage": "https://exampple.com/",
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"antd": "^4.1.4",
"axios": "^0.19.2",
"babel-plugin-import": "^1.13.0",
"customize-cra": "^0.9.1",
"dayjs": "^1.8.23",
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-plugin-flowtype": "^4.6.0",
"gh-pages": "^2.2.0",
"less": "^3.11.1",
"less-loader": "^5.0.0",
"react": "^16.13.1",
"react-app-rewired": "^2.1.6",
"react-dom": "^16.13.1",
"react-ga": "^2.7.0",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"tsutil": "^6.1.1",
"typescript": "^3.8.3"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "netlify deploy",
"deployprod": "netlify deploy --prod",
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject",
"postinstall": "npm run build"
},
"engines": {
"node": "13.7.0",
"npm": "6.13.6"
},
"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"
]
}
}
All I had to do to fix it was update from "antd" version 4.0.0 to the latest version 4.6.3

Resources