Resource Hints — Performance

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…

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:

  1. DNS: looking up the IP address for the domain name.
  2. TCP: opening the connection.
  3. 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">

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>

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:

💡 fetchpriority, modulepreload and Speculation Rules

⚠️ Hints that quietly do nothing

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.

What you will learn