css - 链接 rel ="preload"究竟是如何工作的?

标签 css html browser preloading

Chrome 新版本增加了对<link rel="preload"> 的支持.他们发布了很多引用原始文档的信息。有人可以简单解释一下它是如何工作的,以及与没有 rel="preload" 的情况相比有什么区别吗? .

最佳答案

在最基本的形式中,它将具有 rel="preload"link 设置为高优先级,与预取不同,浏览器可以决定它是否是一个好的不管想法与否,预加载都会强制浏览器这样做。

===更深入的了解:===

这是来自 W3c 的一个片段

Many applications require fine-grained control over when resources are fetched, processed, and applied to the document. For example, the loading and processing of some resources may be deferred by the application to reduce resource contention and improve performance of the initial load. This behavior is typically achieved by moving resource fetching into custom resource loading logic defined by the application - i.e. resource fetches are initiated via injected elements, or via XMLHttpRequest, when particular application conditions are met.

However, there are also cases where some resources need to be fetched as early as possible, but their processing and execution logic is subject to application-specific requirements - e.g. dependency management, conditional loading, ordering guarantees, and so on. Currently, it is not possible to deliver this behavior without a performance penalty.

Declaring a resource via one of the existing elements (e.g. img, script, link) couples resource fetching and execution. Whereas, an application may want to fetch, but delay execution of the resource until some condition is met. Fetching resources with XMLHttpRequest to avoid above behavior incurs a serious performance penalty by hiding resource declarations from the user agent's DOM and preload parsers. The resource fetches are only dispatched when the relevant JavaScript is executed, which due to abundance of blocking scripts on most pages introduces significant delays and affects application performance. The preload keyword on link elements provides a declarative fetch primitive that addresses the above use case of initiating an early fetch and separating fetching from resource execution. As such, preload keyword serves as a low-level primitive that enables applications to build custom resource loading and execution behaviors without hiding resources from the user agent and incurring delayed resource fetching penalties.

For example, the application can use the preload keyword to initiate early, high-priority, and non-render-blocking fetch of a CSS resource that can then be applied by the application at appropriate time:

<!-- preload stylesheet resource via declarative markup -->
<link rel="preload" href="/styles/other.css" as="style">
<!-- or, preload stylesheet resource via JavaScript -->
<script>
var res = document.createElement("link");
res.rel = "preload";
res.as = "style";
res.href = "styles/other.css";
document.head.appendChild(res);
</script>

这是一个真正的in-depth description来自 W3C 规范。

全局支持是good across modern browsers ,约为 93%(截至 2022 年 6 月)。

关于css - 链接 rel ="preload"究竟是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661065/

相关文章:

ios - 带有 flexbox 的 Bootstrap 列在 iOS 和 Safari 上的宽度不正确

jquery - 禁用悬停在特定的单元格上

jquery - 隐藏表列,应用了 colspan ...?

javascript - 单击 anchor 时如何更改元素的样式

css - 在平板设备上设置禁用输入的样式

javascript - 如何制作良好的报告界面

javascript - 如何使用 jQuery 执行脚本直至其加载

html - HTML 选项卡中的 Firebug 浅色 - 如何显示它们

html - 为什么需要提供 React 构建?为什么我不能直接在浏览器中打开它?

html - 单个 HTML 文档中的多个文档类型