简体   繁体   English

部署到 heroku 时,React 应用程序不显示任何内容

[英]React app does not display anything when deployed to heroku

My app was built successfully without any errors.我的应用程序已成功构建,没有任何错误。 However, it only shows up a blank page when I try to open it.但是,当我尝试打开它时,它只显示一个空白页面。 The error on the Chrome dev tools that I got are:我得到的 Chrome 开发工具的错误是:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-eE1k/Cs1U0Li9/ihPPQ7jKIGDvR8fYw65VJw+txfifw='), or a nonce ('nonce-...') is required to enable inline execution.

Refused to load the script 'https://www.google-analytics.com/analytics.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

Refused to load the script 'https://www.pagespeed-mod.com/v1/taas?id=cs&ak=32b001198a46647f164402ebaec7a88c&si=d07acaa3a5ff4a4f99b12b98acafe347&tag=1005&rand=elUWG4o247dNBGXBV31uSeN1epHHQ1Qs&ord=4755781515134192' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

and I have no idea what I did wrong.我不知道我做错了什么。 Does anyone have any idea?有人有什么主意吗? Thanks谢谢

Did you find a solution?你找到解决办法了吗?

I have run into the same problem yesterday and found this thread as well.我昨天遇到了同样的问题,也发现了这个线程。 For me, Helmet was causing this problem.对我来说,Helmet 导致了这个问题。 Do you use it in your express server?你在你的快递服务器中使用它吗?

Helmet 4.0 automatically sets Content Security Policy to a default strict value, while Helmet 3.x didn't do this previously (that's why I encountered this problem yesterday when I updated it to 4.0). Helmet 4.0 自动将 Content Security Policy 设置为默认的 strict 值,而 Helmet 3.x 以前没有这样做(这就是我昨天更新到 4.0 时遇到这个问题的原因)。

So if you have Helmet installed, this should help:因此,如果您安装了 Helmet,这应该会有所帮助:

app.use(helmet({
  contentSecurityPolicy: false,
}));

For more info - https://helmetjs.github.io/欲了解更多信息 - https://helmetjs.github.io/

For me works that:对我来说是这样的:

In server.js adds:在 server.js 中添加:

app.use(
  helmet({
    contentSecurityPolicy: false,
  }),
);

and in package.json into scripts adds:并在 package.json 到脚本中添加:

"build": "INLINE_RUNTIME_CHUNK=false react-scripts build",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM