ASP.NET 6与ReactJS-浏览器不刷新热重新加载反应脚本5.0.0



我有一个ASP。NET 6应用程序,使用Visual Studio 2022ASP.NET Core with React.js模板创建。

ClientApp是使用create-react-app创建的React应用程序。

我已经将我的react-scripts包更新到了5.0.0版本(从4.0.3开始(。AFAIK的一个重要变化是它现在使用了webpack 5。

自从这次更新以来,当我启动ASP。NET应用程序(使用同时启动ASP.NET应用程序和React应用程序的标准运行配置(,热重新加载不会在我更改任何React文件时自动刷新浏览器。如果我手动点击浏览器的刷新按钮或F5,我可以看到更改。唯一的变化是在React文件发生更改后,浏览器不会自行刷新

我使用的是Windows 11。

这是我目前的package.json:

{
"name": "my.app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/jest": "^27.0.3",
"@types/node": "^16.11.9",
"@types/react": "^17.0.35",
"@types/react-dom": "^17.0.11",
"@types/react-router-bootstrap": "^0.24.5",
"bootstrap": "^5.1.3",
"jquery": "^3.5.1",
"merge": "^1.2.1",
"node-sass-chokidar": "^1.5.0",
"npm-run-all": "^4.1.5",
"oidc-client": "^1.9.0",
"react": "^16.0.0",
"react-bootstrap": "^2.0.3",
"react-dom": "^16.0.0",
"react-icons": "^4.3.1",
"react-pro-sidebar": "^0.7.1",
"react-responsive": "^9.0.0-beta.5",
"react-router": "^6.0.2",
"react-router-bootstrap": "^0.26.0",
"react-router-dom": "^6.0.2",
"react-scripts": "^5.0.0",
"rimraf": "^2.6.2"
},
"devDependencies": {
"@testing-library/react": "^12.1.2",
"@types/react-router-dom": "^5.3.2",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"ajv": "^6.9.1",
"cross-env": "^5.2.0",
"eslint": "^7.11.0",
"eslint-config-react-app": "^5.2.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.18.3",
"nan": "^2.14.1",
"prettier": "2.4.1",
"typescript": "^4.5.2"
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"react-app"
]
},
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build-js": "react-scripts build",
"build": "npm-run-all build-css build-js",
"test": "cross-env CI=true react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"lint": "eslint ./src/"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"resolutions": {
"url-parse": ">=1.5.0",
"lodash": ">=4.17.21"
}
}

如果我将react-scripts恢复到4.0.3,它工作正常。但是其他一些软件包(如eslint(与此版本的react-scripts不兼容。

我已经尝试过的

我在SO和GitHub上发现了许多类似的线程,我已经尝试过了:

  • FAST_REFRESH=false添加到.env文件-没有任何更改
  • 将我的start脚本更改为"start": "FAST_REFRESH=false react-scripts start"-我在应用程序启动时遇到此错误:'FAST_REFRESH' is not recognized as an internal or external command
  • 将我的start脚本更改为"start": "CHOKIDAR_USEPOLLING=true react-scripts start"-我在应用程序启动时遇到此错误:'CHOKIDAR_USEPOLLING' is not recognized as an internal or external command

有什么想法吗?

更新

这很可能是CRA5中引入的一个错误:发布

使用WDS_SOCKET_PORT=0将允许解决方案与所有调试配置一起工作。

=============================================

我注意到,在升级到CRA5之后,react-dev客户端开始尊重当前页面的协议。也就是说,如果您在本地使用https调试asp.net核心项目,react dev客户端还会尝试使用默认情况下未启用的wss(通过TLS的websocket(连接到node dev服务器。有几种方法可以解决这个问题,最简单的方法是:

  1. package.json所在的同一文件夹中创建一个名为.env.development的文件
  2. WDS_SOCKET_PORT=<your asp.net core app port>放入您刚刚创建的.env.development中。如果使用的是由dotnet cli生成的SPA模板,则默认情况下<your asp.net core app port>应为5001

这将允许使用UseReactDevelopmentServer中间件将react dev客户端启动的ws连接代理到node dev服务器。

最新更新