带有ReactJS的ASP.NET 6-浏览器在使用Reaction脚本5.0.0进行热重新加载时不刷新 [英] ASP.NET 6 with ReactJS - browser not refreshing on hot reload with react-scripts 5.0.0

查看:25
本文介绍了带有ReactJS的ASP.NET 6-浏览器在使用Reaction脚本5.0.0进行热重新加载时不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ReactJS的ASP.NET 6应用程序,这是一段时间前使用Visual Studio 2022ASP.NET Core with React.js模板创建的。

客户端应用程序是使用create-react-app创建的Reaction应用程序。

我已将react-scripts程序包更新到版本5.0.0(从4.0.3)。AFAIK的一个重大变化是现在使用了webpack 5。

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

我使用的是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不兼容。

我已经尝试的

我在SOGitHub上找到了许多类似的帖子,我已经尝试过了:

  • 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中引入的错误:issue

使用WDS_SOCKET_PORT=0将允许解决方案使用所有调试配置。

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

我注意到,在升级到CRA5之后,REACT开发客户端开始尊重当前页面的协议。 也就是说,如果您在本地使用HTTPS调试您的ASP.NET核心项目,React开发客户端还将尝试使用WSS(TLS上的WebSocket)连接到节点开发服务器,这在默认情况下是不启用的。 有几种方法可以解决这个问题,最简单的方法是:

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

这将允许使用UseReactDevelopmentServer中间件将由React开发客户端启动的%ws连接代理到节点开发服务器。

这篇关于带有ReactJS的ASP.NET 6-浏览器在使用Reaction脚本5.0.0进行热重新加载时不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆