如何使用完整性和跨域预加载脚本 [英] How to preload script using integrity and crossorigin

查看:230
本文介绍了如何使用完整性和跨域预加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对我的Jquery库使用preload并使用以下代码.

I wish to use preload for my Jquery libs and use the following code.

<link rel="preload" href="https://code.jquery.com/jquery-3.4.0.slim.min.js" as="script" integrity="sha256-ZaXnYkHGqIhqTbJ6MB4l9Frs/r7U4jlx7ir8PJYBqbI="
  crossorigin="anonymous">

<script
  src="https://code.jquery.com/jquery-3.4.0.slim.min.js"
  integrity="sha256-ZaXnYkHGqIhqTbJ6MB4l9Frs/r7U4jlx7ir8PJYBqbI="
  crossorigin="anonymous"></script>

但是,这总是在chrome中生成以下警告.

However this always generates the following warnings within chrome.

  1. " https://code的预加载.找到了jquery.com/jquery-3.4.0.slim.min.js ,但由于完整性不匹配而未使用.

  1. A preload for 'https://code.jquery.com/jquery-3.4.0.slim.min.js' is found, but is not used due to an integrity mismatch.

资源 https://code.jquery. com/jquery-3.4.0.slim.min.js 已使用链接预加载进行了预加载,但在窗口加载事件发生后的几秒钟内未使用.请确保它具有适当的as值并且已被有意预加载.

The resource https://code.jquery.com/jquery-3.4.0.slim.min.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate as value and it is preloaded intentionally.

如果我使用标准实现,则下面的代码可以正常工作.

The code below will work fine if I use the standard implementation.

<link rel="preload" href="https://code.jquery.com/jquery-3.4.0.slim.min.js" as="script">

<script>
  src="https://code.jquery.com/jquery-3.4.0.slim.min.js"
</script>

所以我的问题是我是否可以预加载外部库并同时使用跨域和完整性(子资源完整性)?

So my question is can I preload external libs and use the crossorigin and integrity (Subresource Integrity) as well?

谢谢

推荐答案

简短的回答:不能.

具有完整性属性的资源(暂时)无法重用预加载的资源,也可能导致两次提取.链接元素的integrity属性尚未实现,并且有一个开放规范

Resources with an integrity attribute can’t reuse preloaded resources (for now) and can also cause double fetches. The integrity attribute for link elements has not yet been implemented and there’s an open spec issue about it. This means the presence of any integrity metadata will currently discard preloaded resources. In the wild, it can also result in duplicate requests where you have to make a trade-off between security and performance.

这篇关于如何使用完整性和跨域预加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆