3

我有一个已上传到 Google Cloud Project 存储桶中的 JS 文件。我将其用作 CDN。在我向用作 CDN 的 JS 文件添加完整性检查后,我不断收到错误消息

Access to script at someCDN.js from origin requestURL.com has been blocked by CORS policy. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'requestURL.com' is therefore not allowed access

在添加完整性检查之前,我之前能够毫无问题地使用此 CDN。我仔细检查并确保请求的 URL 设置为允许的 Origin 标头,但现在我在添加完整性检查后收到此错误。

我使用 CDN 的方式是<script>动态构建标签并将其附加到 DOM。

  const script = document.createElement('script')
  const parent = document.body

  script.src = 'https://storage.googleapis.com/some/file.js'
  script.integrity = 'sha384-someSRIhashkey'
  script.setAttribute('crossOrigin', 'anonymous')

  parent.appendChild(script)

我生成 HASH 的方式是https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity#Tools_for_generating_SRI_hashes

使用文档中的命令生成哈希后,我将文件作为对象上传到 Google Cloud 存储桶。

4

0 回答 0