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,preloadandprefetch - How to choose between them
- Why
asandcrossoriginmatter - 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.
- HTML Code
- Hint Builder
- Hint Checker
<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>
<link rel="preload" href="/img/hero-1200.webp" as="image">Loading the checker...
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.
| Hint | What the browser does | Cost | Best for |
|---|---|---|---|
dns-prefetch | Looks up the server's address | Very small | Third-party hosts you might use |
preconnect | Looks up the address, connects, and sets up security | Small, but a connection stays open | The few hosts you will use very soon |
preload | Downloads one file for this page, early, at high priority | Real bandwidth | A file the page needs but finds late |
prefetch | Downloads a file at low priority, for later | Real bandwidth, spent on a guess | Likely 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:
- Your page must work without the hints. A hint speeds things up. It should never be required for the page to work.
- 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?
- 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. - I know the server, but not the files. Use
preconnectif you will use the server almost straight away, ordns-prefetchif you might. - The visitor will probably go to a certain page next. Use
prefetchfor that page or file. - The browser will find the file early anyway. Use nothing. A normal image or stylesheet in your HTML is already found early.
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:
modulepreloadis 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.
Add hints one at a time, and measure after each change. Remove any hint that does not clearly help.
Checking That a Hint Workedβ
- Open DevTools, then the Network panel, and reload.
- Find your file, and look at the Priority and Initiator columns. A preloaded file should start early, with an initiator showing the preload.
- Check the Waterfall. A good preload makes the file start near the top, not after a stylesheet or script finishes.
- Watch the Console for warnings about unused preloads or mismatched
crossorigin. - Run Lighthouse or WebPageTest before and after, and compare your key numbers, such as Largest Contentful Paint (LCP).
Best Practicesβ
- Make the file easy to find first. Hints are a fix for late discovery, not a replacement for good HTML.
- Keep the list short. A few well-chosen hints beat a long list.
- Always add
asto a preload, andcrossoriginwhere needed. - Match the hint to the real request, with the same URL, the same
as, and the samecrossorigin. - Use
dns-prefetchas a cheap fallback next topreconnect, for older browsers. - Never rely on a hint for the page to work.
- 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
asvalue - 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.
preconnectanddns-prefetchmake 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
integritywhen it fits. For a preloaded script or style that does not change, theintegrityattribute 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
preconnectmore 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β
- Preloading without
as. - Forgetting
crossoriginon fonts. - Preloading a file the page does not use.
- Hinting at files the browser would find at once.
- Preloading everything, so nothing stands out.
- Preconnecting to a dozen hosts.
- Writing the same hint twice.
- Prefetching private or action URLs.
- Putting a path in a preconnect and thinking it matters.
- Never measuring whether the hint helped.
Practice Timeβ
- In the builder, create a preload for a font. What does it add on its own?
- Create a preconnect with a long URL. What does the note tell you?
- Load the messy head into the checker and fix every problem it reports.
- Open DevTools on a page you built. Is any file discovered late?
- Add a preload for that file, reload, and compare the waterfall.
- Add a preload for a file you do not use, and look in the console. What does Chrome say?
- Find a page where
prefetchwould make sense, and write down why. - 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-prefetchandpreconnectprepare connections.preloadandprefetchdownload files.- Preload is for this page, prefetch is for the next.
- Always add
asto a preload, andcrossoriginfor 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.