In the Lab's online shop, the product data only shows at 1,451 ms. 300 ms of that goes not on downloading but on getting to know the API server first: looking up its address, opening a connection, agreeing on encryption. The browser only starts after app.js runs, even though you knew from the start which server it would talk to.
Resource hints are <link> tags in the <head> that tell the browser early about something it's going to need, so it can start connecting or downloading before it finds that thing on its own. It's like eating at a restaurant: dns-prefetch is looking up the restaurant's address first, preconnect is already walking there and sitting at a table, preload is ordering the main course right now because you'll definitely eat it, and prefetch is asking for a takeaway for tonight, made when the kitchen is quiet.
Why the browser finds out late
The browser only knows a file is needed once it finds it. The problem is that some files are only found late. This is the shop page in the Lab (a network with a 100 ms round trip and a speed of 400 KB per second):
| File | Found when | Done |
|---|---|---|
index.html |
at the start | 450 ms |
style.css, app.js |
the HTML <head> is read, 400 ms |
650 ms, 976 ms |
font inter.woff2 |
the CSS has been read, 650 ms | 863 ms |
data api.myshop.com/products |
app.js has finished running, 1,026 ms |
1,451 ms |
The font is only requested after the CSS is done, because the @font-face lives inside the CSS. The product data is only requested after app.js runs, and since its address is on another origin, the browser still has to connect there first. Resource hints move that work to the front.
preconnect and dns-prefetch: prepare the road
Connecting to a new origin (the protocol, domain and port together, for example https://api.myshop.com) takes three steps, each about one round trip to the server:
- DNS: looking up the IP address for the domain name.
- TCP: opening the connection.
- TLS: agreeing on encryption for HTTPS.
In the Lab that's 3 × 100 ms = 300 ms before the first request can be sent.
<!-- Used very soon: prepare DNS + TCP + TLS now -->
<link rel="preconnect" href="https://api.myshop.com" crossorigin>
<!-- Used later: just look up the address for now -->
<link rel="dns-prefetch" href="https://stats.analytics.com">
preconnectdoes all three steps as soon as the<head>is read. In the Lab, the product data shows at 1,151 ms, not 1,451 ms: 300 ms saved.crossoriginon a preconnect is for an origin reached throughfetch()or for fonts. The browser uses a separate connection for requests like that, so withoutcrossoriginthe prepared connection goes unused and only its DNS helps.dns-prefetchonly does DNS. It's cheaper, and fits an origin used later on. In the Lab, the analytics script is done at 1,326 ms, not 1,426 ms.
preload: download now, definitely used
preload tells the browser to download one file now, at high priority, because this page will definitely use it very soon.
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
as="font"is required: it says what kind of content it is, so the priority is right and the result can be reused. Other common values:style,script,image,fetch. Withoutas, the preload is useless.type="font/woff2"lets a browser that doesn't support that format skip it.crossoriginis required for fonts, even when the file is on your own domain. Fonts are always requested in CORS mode. A preload withoutcrossorigindoesn't match that request, so the font downloads twice.
In the Lab, a correctly preloaded font makes the text show at 726 ms, not 863 ms. Without crossorigin, the text shows at 939 ms: later than with no hint at all, because 30 wasted KB compete for bandwidth too. Chrome also writes a warning in the Console: "A preload for '...' is found, but is not used because the request credentials mode does not match."
prefetch: for the next page
prefetch is for a file the next page might need. The browser downloads it at the lowest priority, after this page is done, and keeps it in the cache.
<link rel="prefetch" href="/assets/products.js">
In the Lab, products.js (the Products page chunk from the code splitting lesson) is quietly downloaded after the page is done. When the user clicks Products, it's already in the cache: a 0 ms wait, not 225 ms.
The difference from preload: preload is for this page and definitely used; prefetch is for the next page and maybe used.
Hints aren't free
Every hint makes the browser work on your guess. A wrong guess wastes effort:
- Too many preloads. In the Lab, if six gallery images (6 × 60 KB) are preloaded too, they all compete for bandwidth with the CSS and the font. Text that showed at 726 ms slips back to 1,026 ms. Preload only 1 to 3 truly critical files, for example the main font and the top image.
- Prefetch that's rarely used. If you prefetch 20 pages but only 2 in 10 users go there, their bandwidth (data plan), CPU and battery are wasted on files nobody uses, and the downloads can compete with the page being opened. Prefetch based on analytics data, not guesses.
- Too many preconnects. A connection that's opened but not used is closed again after a few seconds. Keep it to 2 to 4 origins that are definitely used. The rest only need
dns-prefetch.
💡 fetchpriority, modulepreload and Speculation Rules
fetchpriority="high"on an<img>or a<link rel="preload">raises one file's priority, for example the LCP image.fetchpriority="low"lowers it.<link rel="modulepreload" href="/assets/app.js">is a preload just for ES modules: the file is downloaded and readied as a module right away. Vite uses it for chunks that will definitely be needed.- The old
rel="prerender"has been abandoned. Its replacement is the Speculation Rules API (<script type="speculationrules">), which can prefetch or even prerender the next page. Right now it's mainly supported by Chrome and Edge; browsers that don't support it ignore it, so it's safe to add.
⚠️ Hints that quietly do nothing
- Preloading a font without
crossorigin. The font downloads twice and the text actually shows later. Writecrossorigineven when the font is on your own domain. - Preload without
as. The browser doesn't know what kind of content it is, and the file isn't reused. For a font, writeas="font". - A misspelled
rel. The value must be exactlypreload,preconnect,prefetchordns-prefetch. A typo is silently ignored, with no error. - A preload that isn't used. If a preloaded file isn't used within a few seconds, Chrome warns that it "was preloaded using link preload but not used within a few seconds". That's a sign of wasted bandwidth.
TL;DR: Resource hints are <link> tags in the <head> that move work to the front. preconnect prepares DNS, TCP and TLS to an origin that will definitely be used (in the Lab the product data goes from 1,451 ms to 1,151 ms); dns-prefetch only does DNS, for origins used later. preload downloads a critical file for this page now, always with as (as="font"), and a font also needs crossorigin (text from 863 ms to 726 ms; without crossorigin it's 939 ms). prefetch downloads a file for the next page at low priority. Hints aren't free: too many preloads slow down what's critical, and prefetch that's rarely used wastes the user's data, CPU and battery.
⚡ Prepare the road first
Write:
<link rel="preconnect" href="https://api.myshop.com" crossorigin>
<link rel="dns-prefetch" href="https://stats.analytics.com">
Meaning: preconnect prepares DNS, TCP and TLS to an origin that will definitely be used very soon. In the Lab, the product data shows at 1,151 ms, not 1,451 ms. dns-prefetch only does DNS, for an origin used later on.
⚡ preload: definitely used on this page
Write:
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
Meaning: as is required. A font also needs crossorigin, even when the file is on your own domain. Without it the font downloads twice, and the text shows at 939 ms instead of 726 ms.
⚡ prefetch: maybe used later
Guess first: you prefetch 20 pages, but only 2 in 10 users go there. What does it really cost?
The user's bandwidth (data plan), CPU and battery are wasted on files nobody uses. Prefetch based on analytics data, not guesses.
Meaning: prefetch downloads a file for the next page at the lowest priority. Hints aren't free: preload only 1 to 3 truly critical files.