Subdomain localhost: Invalid host header - reactjs

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/"
}

Related

index.tsx can't resolve App component in React app. .Tsx extensions are not recognised

I have just runned create-react-app, moved some code and components from another project and now I cannot figure out why I get an error that it fails to compile. App component exist exactly in the destination where it can't be found. I suppose there is some hint in console, but I cannot figure out what can be the issue.
import React from "react";
import ReactDOM from "react-dom/client";
import { App } from "./components/App/App";
import reportWebVitals from "./reportWebVitals";
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
Here I add a picture of App component to show You the location of app component.
Can You please suggest what is wrong here ?
Here You can see my package.json
{
"name": "flask_react",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:5000",
"dependencies": {
"#emotion/react": "^11.10.5",
"#emotion/styled": "^11.10.5",
"#mui/material": "^5.11.5",
"#mui/x-date-pickers": "^5.0.15",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"#types/react": "^18.0.27",
"formik": "^2.2.9",
"prettier": "^2.8.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.7.0",
"react-scripts": "5.0.1",
"ts-loader": "^9.4.2",
"typescript": "^4.9.4",
"web-vitals": "^2.1.4",
"yup": "^0.32.11"
},
"scripts": {
"start": "react-scripts start",
"start-backend": "cd backend && env/bin/flask run --no-debugger",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"eslint": "eslint ./src/**/*.{ts,tsx}"
},
"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": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"#types/axios": "^0.14.0",
"#types/jest": "^27.5.2",
"#types/node": "^16.11.64",
"#types/react": "^18.0.21",
"#types/react-beautiful-dnd": "^13.1.2",
"#types/react-dom": "^18.0.6",
"#types/react-icons": "^3.0.0",
"#types/react-router": "^5.1.19",
"#types/react-router-dom": "^5.3.3",
"#typescript-eslint/eslint-plugin": "^5.39.0",
"#typescript-eslint/parser": "^5.39.0",
"env-cmd": "^10.1.0",
"eslint": "^8.24.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.31.8",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^2.7.1",
"typescript": "^4.8.4"
}
}
import { App } from "./components/App/App";
You are destructuring your App import with the curly brackets, which returns undefined. Instead it should be
import App from './components/App/App';
EDIT: as Yuriy pointed out in the comments, since this isn't a default export then what I said above won't fix the problem. I have thought of something else though, what if you specify the extension? Like so
import { App } from "./components/App/App.tsx";
according to the error most probably here you are not including the .tsx extenstion to your extensions array. update your webpack.config.js file as below :
module.exports = {
// ...
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx"]
},
}

No changes getting reflected on changing the path of components in styleguide.config.js (react-styleguidist#13.0.0)

I am using react-styleguidist library for the documentation of my React App.
This is my package.json
`
{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#date-io/date-fns": "^1.3.13",
"#emoji-mart/data": "^1.0.5",
"#emoji-mart/react": "^1.0.1",
"#emotion/react": "^11.9.0",
"#emotion/styled": "^11.8.1",
"#material-ui/core": "^4.12.4",
"#mui/material": "^5.8.1",
"#mui/x-date-pickers": "^5.0.0",
"#mui/x-date-pickers-pro": "^5.0.0",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"bootstrap": "^5.1.3",
"chart.js": "^3.8.2",
"classnames": "^2.3.1",
"dagre": "^0.8.5",
"date-fns": "^2.29.2",
"emoji-mart": "^5.2.1",
"hashmap": "^2.4.0",
"history": "^4.10.1",
"is-url": "^1.2.4",
"moment": "^2.29.4",
"react": "^18.2.0",
"react-awesome-popover": "^6.1.1",
"react-bootstrap": "^2.4.0",
"react-chartjs-2": "^4.3.1",
"react-contextmenu": "^2.14.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-flow-renderer": "^10.3.12",
"react-icons": "^4.3.1",
"react-input-emoji": "^4.1.0",
"react-paginate": "^8.1.3",
"react-pro-sidebar": "^0.7.1",
"react-redux": "^8.0.4",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.1",
"react-secure-storage": "^1.0.21",
"react-select": "^5.4.0",
"react-toastify": "^9.0.8",
"react-viewer": "^3.2.2",
"redux": "^4.2.0",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.1",
"rsuite": "^5.17.1",
"universal-cookie": "^4.0.4",
"video-react": "^0.15.0",
"web-vitals": "^2.1.4"
},
"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"
]
},
"devDependencies": {
"react-styleguidist": "^13.0.0"
}
}
`
This is my styleguide.config.js
`
module.exports = {
components: 'src/Pages/Dashboard/**/*.{js,jsx,ts,tsx}'
}
`
I want react-styleguidist to display all components in Pages as well as Components folder, but it always showing components from Components folder.
Would anyone help me with this?
Thanks in Advance...

Why is the build folder of my react app empty?

I am using create react app to make a react project.It is running without any problem on the localhost.However When I try to build it for production.The build takes like 5 minutes and then the build folder returned is completely empty.
I should say this now that the assets folder inside the app is like 50mb .So dont know if thats causing the issue.If anyone has any idea what might be the problem I will appreciate it.
package.json
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3001",
"dependencies": {
"#amraneze/react-instagram-login": "^3.0.3",
"#emotion/react": "^11.9.0",
"#emotion/styled": "^11.8.1",
"#fortawesome/fontawesome-svg-core": "^6.1.1",
"#fortawesome/free-brands-svg-icons": "^6.1.1",
"#fortawesome/free-solid-svg-icons": "^6.1.1",
"#fortawesome/react-fontawesome": "^0.1.18",
"#mui/material": "^5.6.3",
"#reduxjs/toolkit": "^1.8.1",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.1.1",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"jquery": "^3.6.0",
"puppeteer": "^14.1.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-dropzone": "^14.2.1",
"react-fontawesome": "^1.7.1",
"react-google-login": "^5.2.2",
"react-redux": "^7.2.6",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-slick": "^0.29.0",
"redux": "^4.1.2",
"slick-carousel": "^1.8.1",
"web-vitals": "^2.1.4"
},
"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"
]
}
}

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.

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.

Resources