React-Scripts 開始不接受 js 更改
P粉212114661
P粉212114661 2024-01-10 17:02:48
0
1
431

我有一個使用 create-react-app 引導的 React 應用程式。該應用程式運行在docker容器中,並已部署在子網域中。假設網域是 http://mycompany.com ,那麼我的 React 應用程式位於 http://mycompany.com/users/userid/apps/reactapp 。

有一個反向代理伺服器可以進行 URL 重寫,將基本 URL 變更為子網域 URL。這就是我的 package.json 的樣子 -

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.12.4",
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "@types/classnames": "^2.3.1",
    "@types/jest": "^27.0.3",
    "@types/node": "^16.11.47",
    "@types/react": "^17.0.38",
    "@types/react-dom": "^17.0.11",
    "@types/react-icons": "^3.0.0",
    "@types/react-router-dom": "^5.3.2",
    "@types/sass": "^1.43.1",
    "bootstrap": "^5.1.3",
    "classnames": "^2.3.1",
    "font-awesome": "^4.7.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-icons": "^4.3.1",
    "react-router-dom": "^6.3.0",
    "react-scripts": "5.0.0",
    "register-service-worker": "^1.7.2",
    "typescript": "^4.5.4",
    "web-vitals": "^2.1.2"
  },
  "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": {
    "css-flatten": "^1.0.1",
    "react-hot-loader": "^4.13.1",
    "react-select": "^5.4.0",
    "sass": "^1.45.2"
  }
}

我面臨的問題是,即使在我使用react-scripts start重新啟動開發伺服器後,任何JS檔案變更都不會被反映。只有當我重新啟動 docker 容器時才會反映出來。在瀏覽器中,我檢查過,bundle.js 檔案沒有最新的變更。我已經嘗試過這些事情 -

  1. 刪除node_modules/.cache、node_modeules
  2. #在react-scripts啟動中使用CHOKIDAR_USEPOLLING=true

我可以在開發伺服器日誌中看到,正在偵測到檔案更改,但這些更改並不用於提供給瀏覽器。即使我刪除了一個檔案(因此我可以在開發伺服器日誌中看到錯誤日誌),但即使瀏覽器刷新,應用程式仍然沒有任何更改。

P粉212114661
P粉212114661

全部回覆(1)
P粉287254588

您可能需要將以下內容新增至伺服器端回應標頭

"headers": { 
    "/**": { 
        "Cache-Control": "no-store, no-cache" 
    } 
}

如果問題仍然存在,您可以嘗試清除瀏覽器快取

#相關問題:

  1. 使用 CRA React 進行快取清除
  2. ReactJS:如何防止瀏覽器快取靜態檔案?
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板