前端優化實踐1 Preload, prefetch and other <link> tags
阿新 • • 發佈:2021-06-16
Preload, prefetch and other <link> tags
Published 18 March 2019 · Last updated11 May 2021·Ivan AkulovThere’re lots of ways to improve web performance (seeWeb Performance 101for a full overview). One of those ways is to preload content you’ll need later in advance. Prefetch a CSS file, prerender a full page, or resolve a domain ahead of time – and you won’t have to wait for it when it’s actually needed! Sounds cool.
What’s even cooler is that browsers have a simple built-in way to do all these things. There’re six<link rel>
tags that instruct the browser to preload something:
<link rel="prefetch" href="/style.css" as="style" />
<link rel="preload" href="/style.css" as="style" />
<link rel="preconnect" href="https://example.com" />
<link rel="dns-prefetch" href="https://example.com" />
<link rel="prerender" href="https://example.com/about.html" />
<link rel="modulepreload" href="/script.js" />
Here’s what they each of them does and when to use them.