猿问

内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self'”

我react-create-app用来构建我的 chrome 扩展。当我npm run build在 react-create-app 中使用时出现错误:


拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self'”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-5=')或随机数(“nonce-...”)。


错误 index.html


<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8" />

    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />

    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <meta name="theme-color" content="#000000" />

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

    <link

      rel="stylesheet"

      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"

      crossorigin="anonymous"

    />

    <!--

      manifest.json provides metadata used when your web app is installed on a

      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/

    -->

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

    <!--

      Notice the use of %PUBLIC_URL% in the tags above.

      It will be replaced with the URL of the `public` folder during the build.

      Only files inside the `public` folder can be referenced from the HTML.


      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will

      work correctly both with client-side routing and a non-root public URL.

      Learn how to configure a non-root public URL by running `npm run build`.

    -->

    <title>React App</title>

  </head>

  <body>

    <noscript>You need to enable JavaScript to run this app.</noscript>

    <div id="root"></div>


守着星空守着你
浏览 1516回答 3
3回答

慕妹3242003

经过几个小时的沮丧之后,我找到了一个可行的解决方案。Mac 和 PC 之间在运行脚本方面存在差异。我发现的很多答案都带有“集合”和“不带”。有“&&”和没有......没有一个为我工作。TLDR:使用“cross-env” npm 包,如下所示:cross-env&nbsp;INLINE_RUNTIME_CHUNK=false&nbsp;react-scripts&nbsp;build这适用于 PC,也可能适用于 Mac。当然,你需要npm install --save-dev cross-env之前。

翻过高山走不出你

在 中package.json,将"build"脚本更新为:"build":&nbsp;"INLINE_RUNTIME_CHUNK=false&nbsp;react-scripts&nbsp;build"

慕丝7291255

对于那些仍然有问题的INLINE_RUNTIME_CHUNK not recognized as a command,您需要将set添加到构建脚本中。"build":&nbsp;"set&nbsp;INLINE_RUNTIME_CHUNK=false&&react-scripts&nbsp;build"这样INLINE_RUNTIME_CHUNK值将在构建时设置,而不是在环境变量中查找。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答