reactjs 部署后响应白色,错误404 chunk.js

lf5gs5x2  于 2022-12-18  发布在  React
关注(0)|答案(1)|浏览(177)

我是React的新手,我知道有很多答案,但没有一个适合我。我创建了一个React应用程序,托管在AWS的S3桶上,由AWS Cloudfront部署。
有时,当我使用$ npm run deploy部署应用程序并转到浏览器时,会显示一个白色屏幕,并显示以下错误:控制台上的块.js 404。(https://i.stack.imgur.com/Ri7qX.png
这是我的“package.json":我尝试设置我的“主页":与:

  • “/”
  • “.”
  • “https://website.com/“
  • “https://website.com/django-react-portal“

包.json

"name": "django-react-portal",
"version": "0.1.0",
"private": true,
"homepage": "/",
"dependencies": {
    "@material-ui/core": "^4.11.0",
    "@material-ui/icons": "^4.9.1",
    "@material-ui/lab": "^4.0.0-alpha.56",
    "animate.css": "^3.7.0",
    "antd": "^4.6.2",
    "axios": "^0.18.1",
    "bootstrap": "^4.5.2",
    "build": "^0.1.4",
    "clsx": "^1.1.1",
    "dropzone": "^5.7.2",
    "mailgun-js": "^0.22.0",
    "material-ui": "^0.20.2",
    "material-ui-dropzone": "^3.4.0",
    "mdbreact": "^4.27.0",
    "node-sass": "^4.14.1",
    "popper.js": "^1.14.3",
    "prop-types": "^15.7.1",
    "react": "^16.13.1",
    "react-axios": "^2.0.3",
    "react-bootstrap": "^1.3.0",
    "react-bootstrap4-form-validation": "^1.0.10",
    "react-datepicker": "^3.1.3",
    "react-dom": "^16.8.1",
    "react-dropzone": "^11.0.3",
    "react-dropzone-uploader": "^2.11.0",
    "react-google-recaptcha-v3": "^1.5.2",
    "react-icofont": "0.0.6",
    "react-icons": "^3.11.0",
    "react-image-lightbox": "^5.1.0",
    "react-mailchimp-subscribe": "^2.1.0",
    "react-modal-video": "^1.2.1",
    "react-owl-carousel3": "^2.2.5",
    "react-page-loading": "^1.0.2",
    "react-recaptcha-hook": "^1.1.1",
    "react-redux": "^7.2.1",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^3.4.3",
    "react-scroll": "^1.7.10",
    "react-scroll-up-button": "^1.6.4",
    "react-visibility-sensor": "^5.0.2",
    "react-wow": "^1.0.0",
    "reactstrap": "^8.7.1",
    "redux": "^4.0.5",
    "redux-form": "^8.3.6",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^1.2.1",
    "unidecode": "^0.1.8"
},

这是我的“App.js":

function App() {
  return (
    <BrowserRouter>
      <Page loader={"bubble-spin"} color={"orange"} size={25}>
        <Switch>
          <Route path="/" component={Particuliers} />
          <Route path="/carrieres" component={Carrieres} />
          {/* <Route path="/particuliers" component={} /> */}
          <Route path="/entreprises" component={Entreprises} />
          <Route path="/test" component={HeaderAdmin} />
          <Route path="/concours" component={ConcoursCreate} />
          <Route path="/confirmation-concours" component={ConfirmationConcours} />
          <Route path="/politique-de-confidentialite" component={PolitiqueConfidentialite} />
          <Route path="/succes" component={SuccesCarrieres} />
          <Route path="/login" component={Login} />
          <Route>
            <Layout>
              <Switch>
                <PrivateRoute exact path="/careers" component={CareerList} />
                <PrivateRoute
                  exact
                  path="/careers/:clientId"
                  component={CareerDetail}
                />
                <PrivateRoute exact path="/concours-list" component={ConcoursList} />
                <PrivateRoute exact path="/livestats" component={LiveStats} />
              </Switch>
            </Layout>
          </Route>
        </Switch>
      </Page>
    </BrowserRouter>
  );
}
export default App;

这是我的“index.js":

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route } from "react-router-dom";
import "semantic-ui-css/semantic.min.css";
import { StateProvider } from "./StateProvider";
import reducer, { initialState } from "./reducer";

import App from "./App";

ReactDOM.render(
  <BrowserRouter basename={"/"}>
    <StateProvider initialState={initialState} reducer={reducer}>
      <Route path="/" component={App} />
    </StateProvider>
  </BrowserRouter>,
  document.getElementById("root")
);


奇怪的是,如果我等上5-10个小时,应用程序就会毫无错误地出现,就像白色屏在部署后随机出现,而不是每次部署后。
谢谢

vsmadaxz

vsmadaxz1#

这是AWS Cloudfront的缓存问题。解决方案是创建自定义策略:
1.转到AWS云前端
1.选择您的发行版
1.转到行为选项卡

  • 创建两个行为(一个用于/,一个用于index.html)
  • 在“缓存策略和源请求策略”中,选择“创建策略”或在“策略”模块中创建策略。
  • 在TTL设置中,设置:
  • 最小TTL、最大TTL和默认TTL为0
  • 取消选中压缩支持中的Brotli和Gzip

相关问题