Webpack with Proxy - The development server has disconnected - reactjs

In my React app, I'm getting the following error about a minute after connection is established:
The development server has disconnected.
Refresh the page if necessary.
If I refresh, it connects again, only to disconnect after a minute again.
Webpack was installed via create-reac-app.
This is my package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-alert": "^5.5.0",
"react-dom": "^16.8.6",
"react-hot-loader": "^4.5.3",
"react-html-parser": "^2.0.2",
"react-player": "^1.13.0",
"react-router-dom": "^5.0.0",
"react-scripts": "^3.3.0",
"react-transition-group": "^4.3.0",
"spotify-web-api-js": "^1.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2"
}
}
I use a nginx proxy reverse, with the following configuration:
server {
listen 80;
location / {
proxy_pass http://client:3000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /users {
proxy_pass http://web:5000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
If I go to http://localhost:3000/, I don't face the issue, so it must be related to proxy.
Log:
client_1 ℹ 「wds」: Project is running at http://171.13.0.12/
client_1 ℹ 「wds」: webpack output is served from /
client_1 ℹ 「wds」: Content not from webpack is served from /usr/src/app/public
client_1 ℹ 「wds」: 404s will fallback to /index.html
Starting the development server...
This is my webpack.config.js file:
https://pastebin.com/HF78WjLL
EDIT: I have tried, based on the answer below, to add my network to package.json, like so:
"scripts": {
"start": "HOST='0.0.0.0' react-scripts start",
...,
}
But the same error persists, just like before: it connects and quickly disconnects.
How should I fix this?

Well, react-scripts 3.3.0 working with proxy was the problem, indeed.
According to the recent issue: https://github.com/facebook/create-react-app/issues/8203,
This is related to nginx's default proxy_read_timeout 60s rule. It seems that prior react-scripts versions would reload the websocket connection when it timed out after 60s.
So, adding the following lines to nginx's dev.conf, like so:
location / {
proxy_pass http://client:3000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
# the following two timeout rules fix CRA WDS disconnects after 60s
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
solved the problem.

The webpack dev server runs on localhost by default. Localhost is only accessible from within the container. To allow it to be accessible from the host, (i.e. outside of the container), you need to make webpack serve the bundles on a publicly reachable address, 0.0.0.0.
Add this to your config:
const config = {
...,
devServer: {
contentBase: './dist',
port: process.env.PORT || 3000,
host: '0.0.0.0'
}
...,
}
module.exports = config

Related

Containerization Frontend project cannot auto-refresh after code changes

I have a frontend project that was containerized by docker and docker-compose.yml, a few months ago. It was good and can automatically refresh the page localhost:8080 after I made the code changes, Below shows the main code,
docker-compose.yml
client:
build:
context: ./client
dockerfile: ./docker/local/Dockerfile
restart: on-failure
volumes:
- ./client:/app
- /app/node_modules
networks:
- myapp
nginx:
restart: always
depends_on:
- api
volumes:
- static_volume:/app/staticfiles
- media_volume:/app/mediafiles
build:
context: ./docker/local/nginx
dockerfile: Dockerfile
ports:
- "8080:80"
networks:
- myapp
nginx configurations
upstream client {
server client:3000;
}
server {
location /ws {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://client;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
.env file
WDS_SOCKET_PORT=0
FAST_REFRESH=false
CHOKIDAR_USEPOLLING=true
app.jsx
import React from "react";
const App = () => {
return (
<div className="App">
<h1>Hello World</h1>
</div>
);
};
export default App;
package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#reduxjs/toolkit": "^1.8.2",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^14.2.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-redux": "^8.0.2",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
}
After I make code changes, I can get into the container, and the code does change inside and volume is working. I have no idea why the front end package will not refresh as it was few months ago. I re-installed docker and switched a new pc, but no luck still.
Can anyone provide me some insights?
This is the issue of webpack, if we add WATCHPACK_POLLING=true to the .env file, the auto refresh will be working properly.

React app behind nginx reverse proxy css is text/plain. CSS not applied

I have a react app with a css file. It is behind nginx/1.23.0. It's a nginx container.
App loads fine but without css applied.
css is loaded but:
I have tried with include mime.types which is at same folder as nginx.conf.
btw. This all works with only http nginx proxy but not with https.
Full nginx.conf
events {
worker_connections 1024;
}
http{
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /certs/certnew.cer;
ssl_certificate_key /certs/private.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log error;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /api/ {
proxy_pass http://backend:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /socket-traffic/ {
proxy_pass http://backend:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
OK, the problem seemed to be chrome cache and lack of include mime.types; where it first loaded the css as text\plain and it did not change anymore even after adding mime.types. Only after cache purge

Deploy React Craco app to GCP Cloud Run Error: React Refresh runtime should not be included in the production bundle

I am trying to deploy a React app with Craco to GCP Cloud Run. I am using the Cloud Code in Visual Studio Code to deploy. It say deployment is successful, but I am getting really weird error when viewing in browser, it runs locally just fine, please help.
For build, I am using the remote build option in GCP, it autmatically create a dockerfile and use nginx to build I guess? It's really confusing.
My package.json
{
"name": "material-app",
"version": "3.1.0",
"description": "Material App Pro - React Admin & Dashboard Template",
"author": "Bootlab <support#bootlab.io>",
"private": true,
"license": "https://material-ui.com/store/license/",
"scripts": {
"start": "craco start",
"build": "craco build",
"eject": "craco eject",
"lint": "eslint src --ext .js,.jsx --fix"
},
"browserslist": [
"> 0.5%",
"last 2 versions",
"Firefox ESR",
"not dead",
"not IE 11"
],
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"dependencies": {
"#auth0/auth0-spa-js": "1.18.0",
"#craco/craco": "6.3.0",
"#date-io/date-fns": "2.11.0",
"#fullcalendar/core": "5.9.0",
"#fullcalendar/daygrid": "5.9.0",
"#fullcalendar/interaction": "5.9.0",
"#fullcalendar/react": "5.9.0",
"#mui/icons-material": "5.0.0",
"#mui/lab": "5.0.0-alpha.47",
"#mui/material": "5.0.0",
"#mui/styled-engine-sc": "5.0.0",
"#mui/styles": "5.0.0",
"#mui/system": "5.0.0",
"#mui/x-data-grid": "5.0.0-beta.1",
"#reduxjs/toolkit": "1.6.1",
"amazon-cognito-identity-js": "5.1.1",
"axios": "0.21.4",
"axios-mock-adapter": "1.20.0",
"chart.js": "3.5.1",
"css-vendor": "2.0.8",
"csvtojson": "^2.0.10",
"d3": "^7.1.1",
"date-fns": "2.24.0",
"deepmerge": "4.2.2",
"faker": "5.5.3",
"firebase": "8.10.0",
"formik": "2.2.9",
"google-map-react": "2.1.10",
"history": "5.0.1",
"i18next": "20.6.1",
"jsonwebtoken": "8.5.1",
"jss": "10.8.0",
"jwt-decode": "3.1.2",
"polished": "4.1.3",
"react": "17.0.2",
"react-app-polyfill": "2.0.0",
"react-chartjs-2": "3.0.4",
"react-dom": "17.0.2",
"react-dragula": "1.1.17",
"react-feather": "2.0.9",
"react-helmet-async": "1.1.2",
"react-i18next": "11.12.0",
"react-jvectormap": "0.0.16",
"react-perfect-scrollbar": "1.5.8",
"react-quill": "2.0.0-beta.4",
"react-redux": "7.2.5",
"react-router-dom": "6.0.0-beta.4",
"react-scripts": "4.0.3",
"react-syntax-highlighter": "15.4.4",
"redux": "4.1.1",
"redux-thunk": "2.3.0",
"styled-components": "5.3.1",
"web-vitals": "2.1.0",
"yup": "0.32.9",
"craco-alias": "3.0.1"
},
"devDependencies": {
"#babel/preset-env": "7.15.6",
"redux-devtools-extension": "2.13.9"
},
"resolutions": {
"jss": "10.8.0"
},
"optionalDependencies": {
"fsevents": "^2.3.2"
}
}
My Craco config file:
const CracoAlias = require("craco-alias");
module.exports = {
plugins: [
{
plugin: CracoAlias,
options: {
source: "options",
baseUrl: "./",
aliases: {
"#mui/styled-engine": "./node_modules/#mui/styled-engine-sc",
},
},
},
],
};
My .docker file:
# build environment
FROM node:14-alpine as react-build
WORKDIR /app
COPY . ./
RUN yarn
RUN yarn build
# server environment
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template
COPY --from=react-build /app/build /usr/share/nginx/html
ENV PORT 8080
ENV HOST 0.0.0.0
EXPOSE 8080
CMD sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
My nginx.inf
server {
listen $PORT;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
}
The error I am getting:
Error: React Refresh runtime should not be included in the production bundle.
./node_modules/react-refresh/cjs/react-refresh-runtime.production.min.js
node_modules/react-refresh/cjs/react-refresh-runtime.production.min.js:10
__webpack_require__
/workspace/webpack/bootstrap:856
What does it mean?
Okay fixed it, modify the start script to this:
node server.js
Then add a server.js file in your root directory with this content:
//server.js
const express = require('express');
const favicon = require('express-favicon');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.get('/ping', function (req, res) {
return res.send('pong');
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);
It works now, craco was definitely the problem.

"Invalid Host header" Kubernetes cluster on Digital Ocean with React

My website was working fine before I made my most recent changes on my react app. When I deployed the changes, "Invalid Host header" appears on the webpage.
This is my code structure:
What can I configure to make this error go away?
This is my package.json file:
{
"name": "client",
"proxy": "http://127.0.0.1:4000",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.11.2",
"#material-ui/icons": "^4.11.2",
"#material-ui/styles": "^4.11.2",
"#testing-library/jest-dom": "^5.11.6",
"#testing-library/react": "^11.2.2",
"#testing-library/user-event": "^12.6.0",
"axios": "^0.21.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.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"
]
}
}
This is the log of my pod:
> client#0.1.0 start
> react-scripts start
ℹ 「wds」: Project is running at http://10.244.1.23/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...
Compiled successfully!
You can now view client in the browser.
Local: http://localhost:3000
On Your Network: http://10.244.1.23:3000
Note that the development build is not optimized.
To create a production build, use npm run build.
Thank you!
So I solved the issue by doing the following steps:
npm run eject in my react app folder.
go to config folder's webpackDevServer.config.js file and change
disableHostCheck:!proxy || process.env.DANGEROUSLY_DISABLE_HOST_CHECK === 'true',
to
disableHostCheck: true,

Deploy create-react-app-typescript on Google App Engine

I am triying to deploy an application created using CRA with Typescript into the Google Cloud App Engine service, however, for some reason, the appspot link keeps loading forever until a 502 Bad Gateway appears (the error appears sometimes, normally it just keeps loading..)
I've already checked several tutorials and questions without success.
Deploy create-react-app on Google App Engine
https://medium.com/tech-tajawal/deploying-react-app-to-google-app-engine-a6ea0d5af132
The app.yaml is the following:
env: flex
runtime: nodejs
handlers:
- url: /static/js/(.*)
static_files: build/static/js/\1
upload: build/static/js/(.*)
- url: /static/css/(.*)
static_files: build/static/css/\1
upload: build/static/css/(.*)
- url: /static/media/(.*)
static_files: build/static/media/\1
upload: build/static/media/(.*)
- url: /(.*\.(json|ico))$
static_files: build/\1
upload: build/.*\.(json|ico)$
- url: /
static_files: build/index.html
upload: build/index.html
- url: /.*
static_files: build/index.html
upload: build/index.html
env_variables:
REACT_APP_DEV_API_URL: "......"
REACT_APP_MAP_API_KEY: "........"
# [END app_yaml]
Logs of the app engine (It seems it's calling the 'npm run start' command each time I open a page)
Of course, my application works fine in dev mode and also it doesn't seem to be any problem in the deployment logs.
If someone has experienced this problem before, please let me know how to solve it. Thanks in advance.
package.json:
{
"name": "testing-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"#types/google-map-react": "^1.1.5",
"#types/jest": "^24.0.0",
"#types/lodash": "^4.14.149",
"#types/node": "^12.0.0",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"#types/react-helmet": "^5.0.15",
"#types/react-router-dom": "^5.1.3",
"#types/styled-components": "^5.0.0",
"#types/swiper": "^5.2.1",
"axios": "^0.19.2",
"google-map-react": "^1.1.6",
"lodash.differenceby": "^4.8.0",
"lodash.throttle": "^4.1.1",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-circular-progressbar": "^2.0.3",
"react-dom": "^16.12.0",
"react-id-swiper": "^3.0.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"semantic-ui-react": "^0.88.2",
"styled-components": "^5.0.1",
"swiper": "^5.3.6",
"typescript": "~3.7.2"
},
"devDependencies": {
"#typescript-eslint/eslint-plugin": "^2.14.0",
"#typescript-eslint/parser": "^2.14.0",
"cross-env": "^6.0.3",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.9.0",
"eslint-config-react-app": "^5.1.0",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-react-hooks": "^2.3.0",
"gh-pages": "^2.2.0",
"jest": "^24.9.0",
"lint-staged": "^9.5.0",
"pre-commit": "^1.2.2",
"prettier": "^1.19.1"
},
"lint-staged": {
"*.js": [
"npm run lint:eslint:fix",
"git add --force"
],
"*.json": [
"prettier --write",
"git add --force"
]
},
"pre-commit": "lint:staged",
"resolutions": {
"serialize-javascript": "^2.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build:clean": "rimraf ./build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint:staged": "lint-staged",
"lint:eslint": "eslint --ignore-path .gitignore",
"lint:eslint:fix": "eslint --ignore-path .gitignore --fix",
"prettify": "prettier --write"
},
"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"
]
}
}
The project structure is the typical CRA with containers-components (Ignore the nginx.conf and dockerfile which I was using for testing another way with Gcloud run at the time of the photo)
Edit: Added project structure and package.json
Solved (May 18, 2020)
I changed my approach and go for a custom runtime and it worked out.
Here is the configuration I used if anyone encounter this problem in the future.
Change the runtime to custom in the App.yaml
<!-- language: lang-html -->
env: flex
runtime: custom
And include a Dockerfile with a nginx.conf for the runtime management
Dockerfile:
FROM node:8-alpine as react-build
WORKDIR /app
COPY . ./
RUN npm install
RUN npm run build
# server environment
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template
ENV PORT 8080
ENV HOST 0.0.0.0
RUN sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf"
COPY --from=react-build /app/build /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
server {
listen $PORT;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
}
This configuration appears in one of the tutorials here: https://cloud.google.com/community/tutorials/deploy-react-nginx-cloud-run
Thank you to #Nibrass H for suggesting some possible solutions.

Resources