Skip to main content

Resource Hints in HTML

Normally a browser discovers what it needs as it reads: it finds a stylesheet, fetches it, finds an image, fetches that. This is fair, but sometimes the browser finds out about something too late. A font named inside a CSS file, a background image set in a stylesheet, a server your page will call after a script runs: all of these are discovered late.

Resource hints are small instructions in your HTML that let you tell the browser what is coming, so it can get ready earlier. They are among the cheapest speed-ups you can add, and also among the easiest to misuse. This lesson shows you the four main hints, how they differ, and when to leave them out.

What You Will Learn​

  • What a resource hint is, and why it is only a hint
  • The four main hints: dns-prefetch, preconnect, preload and prefetch
  • How to choose between them
  • Why as and crossorigin matter
  • How too many hints can make a page slower
  • How to check in DevTools that a hint helped

The Big Idea​

All four hints are written with the same element, link, and the rel attribute chooses the job.

index.html
<head>
<meta charset="utf-8">
<title>The Bread Bin</title>

<!-- 1. Only look up the address of a server we might use -->
<link rel="dns-prefetch" href="https://analytics.example.com">

<!-- 2. Open a full connection to a server we will definitely use -->
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

<!-- 3. Download a file this page needs, now -->
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>

<!-- 4. Quietly download a file the NEXT page will probably need -->
<link rel="prefetch" href="/checkout">

<link rel="stylesheet" href="/site.css">
</head>

In the builder, switch between the four hints and notice what changes: as appears for preload and prefetch, and the notes change with the hint. In the checker, load the messy head and fix every problem it reports.

The Four Hints at a Glance​

The four hints sit on a ladder from cheapest and safest to most costly. The first two prepare a connection. The last two download a file.

HintWhat the browser doesCostBest for
dns-prefetchLooks up the server's addressVery smallThird-party hosts you might use
preconnectLooks up the address, connects, and sets up securitySmall, but a connection stays openThe few hosts you will use very soon
preloadDownloads one file for this page, early, at high priorityReal bandwidthA file the page needs but finds late
prefetchDownloads a file at low priority, for laterReal bandwidth, spent on a guessLikely next pages or files

You will go deeper into each one in its own lesson: Preload, Prefetch, DNS Prefetch, and Preconnect.

They Are Only Hints​

The word matters. A resource hint is a request, not a command. A browser may act on it, delay it, or ignore it, depending on the connection, the device, settings such as data saving, and how busy it already is. Support also differs between browsers, so always check MDN for the hint you plan to use.

Two things follow from this:

  1. Your page must work without the hints. A hint speeds things up. It should never be required for the page to work.
  2. Test, do not assume. A hint that helps on your fast laptop might do nothing, or even hurt, on a slow phone.

Which One Do I Need?​

Ask two questions: is the file for this page or the next? and do I know exactly which file?

  1. I know the exact file, this page needs it, and the browser will find it late. Use preload. Typical examples are a font named inside CSS, or a hero image set as a CSS background.
  2. I know the server, but not the files. Use preconnect if you will use the server almost straight away, or dns-prefetch if you might.
  3. The visitor will probably go to a certain page next. Use prefetch for that page or file.
  4. The browser will find the file early anyway. Use nothing. A normal image or stylesheet in your HTML is already found early.
tip

The best resource hint is often no hint at all. If you can move a file earlier in the HTML, or put an image in the HTML instead of in CSS, the browser finds it by itself, and you have one less thing to maintain.

Two Details That Trip People Up​

The as attribute​

A preload must say what kind of file it is, using as. The browser uses this to give the file the right priority, and to send the right request headers. Without it, the browser may fetch the file twice or give it the wrong priority.

<link rel="preload" href="/img/hero.webp" as="image">
<link rel="preload" href="/css/fonts.css" as="style">
<link rel="preload" href="/js/app.js" as="script">

The crossorigin attribute​

Fonts, and anything fetched in "CORS mode", need crossorigin, even when they come from your own site. If the hint and the real request do not match, the browser cannot reuse the preloaded file, and downloads it again.

<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>

The same idea applies to preconnect: if the files will be fetched in CORS mode, add crossorigin to the hint, so the open connection can be reused.

Hints in Other Places​

You can send the same hints from the server, as an HTTP header, which is useful when you cannot edit the HTML:

Link: </img/hero.webp>; rel=preload; as=image

Two related ideas you may meet later:

  • modulepreload is a version of preload made for JavaScript modules (<link rel="modulepreload" href="/app.js">).
  • Speculation rules are a newer way to prefetch or even pre-render a whole next page. Support is not universal, so check current browser support before you rely on it.

When Hints Hurt​

Hints are not free. Each one uses bandwidth, a connection, or the browser's attention.

  • Preload too much and files compete with each other, and with the files that matter. If everything is high priority, nothing is.
  • Preconnect to many hosts and you open connections that may sit unused. Browsers close idle connections after a short time, so a hint for something used much later can be wasted.
  • Preload a file you do not use and Chrome will warn in the console that the file was preloaded but not used soon after load. That is a sign to remove the hint.
  • Prefetch on a slow or metered connection can cost visitors data for pages they never open.
warning

Add hints one at a time, and measure after each change. Remove any hint that does not clearly help.

Checking That a Hint Worked​

  1. Open DevTools, then the Network panel, and reload.
  2. Find your file, and look at the Priority and Initiator columns. A preloaded file should start early, with an initiator showing the preload.
  3. Check the Waterfall. A good preload makes the file start near the top, not after a stylesheet or script finishes.
  4. Watch the Console for warnings about unused preloads or mismatched crossorigin.
  5. Run Lighthouse or WebPageTest before and after, and compare your key numbers, such as Largest Contentful Paint (LCP).

Best Practices​

  1. Make the file easy to find first. Hints are a fix for late discovery, not a replacement for good HTML.
  2. Keep the list short. A few well-chosen hints beat a long list.
  3. Always add as to a preload, and crossorigin where needed.
  4. Match the hint to the real request, with the same URL, the same as, and the same crossorigin.
  5. Use dns-prefetch as a cheap fallback next to preconnect, for older browsers.
  6. Never rely on a hint for the page to work.
  7. Re-check after redesigns, since a hint for a file you no longer use is pure waste.

Quick Checklist​

  • Each hint points to something the page really uses (or will probably use next)
  • Every preload has an as value
  • Font preloads and font-host preconnects have crossorigin
  • No hint is repeated
  • Only a few preconnects, for the hosts that matter most
  • No private or one-time URLs are prefetched
  • DevTools shows the hint helped, and the console shows no warnings

Security and Privacy Notes​

  • Hints contact other servers early. preconnect and dns-prefetch make the visitor's browser contact a third party before the visitor does anything, which shares their network address and some connection details. If you need consent before contacting certain services (analytics or advertising, for example), do not hint those hosts until consent is given. This is not legal advice, so check the rules that apply to you.
  • Prefetch only public pages. A prefetched address is requested whether or not the visitor ever goes there. Never prefetch a logout link, an action link, or any URL that changes something or is private.
  • Use integrity when it fits. For a preloaded script or style that does not change, the integrity attribute lets the browser check the file is the one you expect.
  • Do not leak secrets in URLs. Hint URLs are visible in the page source and can end up in server logs.

Global Audience Notes​

  • Slow networks benefit most, and suffer most from waste. A needless preload costs a visitor on a limited plan real data.
  • Distant servers add delay to every new connection, which makes preconnect more valuable for visitors far from your host.
  • Some third-party hosts are slow or blocked in some countries. A preconnect to a host that never answers wastes time, so make sure the page works without it.
  • Test from other places and on slow connections, because a hint that helps in one region may not help in another.

Common Mistakes to Avoid​

  1. Preloading without as.
  2. Forgetting crossorigin on fonts.
  3. Preloading a file the page does not use.
  4. Hinting at files the browser would find at once.
  5. Preloading everything, so nothing stands out.
  6. Preconnecting to a dozen hosts.
  7. Writing the same hint twice.
  8. Prefetching private or action URLs.
  9. Putting a path in a preconnect and thinking it matters.
  10. Never measuring whether the hint helped.

Practice Time​

  1. In the builder, create a preload for a font. What does it add on its own?
  2. Create a preconnect with a long URL. What does the note tell you?
  3. Load the messy head into the checker and fix every problem it reports.
  4. Open DevTools on a page you built. Is any file discovered late?
  5. Add a preload for that file, reload, and compare the waterfall.
  6. Add a preload for a file you do not use, and look in the console. What does Chrome say?
  7. Find a page where prefetch would make sense, and write down why.
  8. Bonus: run Lighthouse before and after your change, and compare LCP.

Quick Recap​

Click a question to check your answer.

1. What is a resource hint?

A link element that tells the browser about a file or server before it would normally find out. It is a request, and the browser may ignore it.

2. What is the difference between preconnect and dns-prefetch?

dns-prefetch only looks up the server's address. preconnect also opens the connection and sets up security, so it saves more time but costs more.

3. What is the difference between preload and prefetch?

preload fetches a file this page needs, early, at high priority. prefetch fetches a file the next page may need, quietly, at low priority.

4. Why does a preload need as?

It tells the browser what kind of file it is, so it can set the right priority and request headers. Without it the file may be fetched twice or given the wrong priority.

5. When do you need crossorigin?

For fonts (always, even from your own site) and for any file fetched in CORS mode. The hint must match the real request, or the preloaded file cannot be reused.

6. How can hints make a page slower?

Too many preloads compete with important files, unused connections and downloads waste bandwidth, and prefetching on a slow connection costs data for pages nobody opens.

Key Takeaways​

  • Resource hints tell the browser what is coming, so it can prepare earlier.
  • They are hints, not commands, and your page must work without them.
  • dns-prefetch and preconnect prepare connections. preload and prefetch download files.
  • Preload is for this page, prefetch is for the next.
  • Always add as to a preload, and crossorigin for fonts.
  • Fewer, better hints win. Measure each one, and remove what does not help.
  • Mind privacy: early connections reveal visitors to third parties, and prefetch must never touch private URLs.

Next up: Preload, where you will fetch the most important file on your page as early as possible.