首页 » 记事本 » Preload与Resource Hints

Preload与Resource Hints

preload

<!-- 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>
<link rel="preload" href="/assets/font.woff2" as="font" type="font/woff2">
<link rel="preload" href="/style/other.css" as="style">

<link rel="preload" href="//example.com/resource" as="fetch" crossorigin>
<link rel="preload" href="https://fonts.example.com/font.woff2" as="font" crossorigin type="font/woff2">

crossorigin属性是一个CORS settings attribute。它旨在与外部资源链接一起使用。用于启用CORS的资源的Preload links,例如具有crossorigin属性的fonts或images,必须包含一个crossorigin属性,以便正确使用该资源。

<script>
function preloadFinished(e) { ... }
function preloadError(e) { ... }
</script>
<!-- listen for load and error events -->
<link rel="preload" href="app.js" as="script" onload="preloadFinished()" onerror="preloadError()">
<link rel="preload" href="//example.com/widget.html" as="document">

<script>
var res = document.createElement("link");
res.rel = "preload";
res.as = "document";
res.href = "/other/widget.html";
document.head.appendChild(res);
</script>

本站preload前与preload后

Resource Hints

DNS Prefetch

<link rel="dns-prefetch" href="//example.com">

Preconnect

<link rel="preconnect" href="//example.com">
<link rel="preconnect" href="//cdn.example.com" crossorigin>

Prefetch

<link rel="prefetch" href="//example.com/next-page.html" as="html" crossorigin="use-credentials">
<link rel="prefetch" href="/library.js" as="script">

Prerender

<link rel="prerender" href="//example.com/next-page.html">

参考链接

此文章发表在 记事本. 将 固定链接 加入收藏.