DNS Prefetch in HTML
Before your browser can fetch anything from a server, it has to find that server. Humans use names such as cdn.example.com. Computers use numbers. The step that turns one into the other is called a DNS lookup, and it takes time, every time the browser meets a new host.
dns-prefetch is the smallest resource hint there is. It tells the browser, "do the lookup for this host now, while you have a spare moment." It does nothing else, and costs almost nothing. This lesson shows you what it saves, where it is useful, and why it is often paired with its bigger sibling, preconnect.
What You Will Learnβ
- What a DNS lookup is, and why it takes time
- What
dns-prefetchdoes, and what it does not do - How it differs from
preconnect - Which hosts deserve the hint, and which do not
- How to use it as a fallback next to
preconnect - How to see DNS time in DevTools
The Big Ideaβ
A DNS prefetch is a link element with the address of a host. Only the host matters, not the file.
- HTML Code
- Connection Lab
<head>
<meta charset="utf-8">
<title>The Bread Bin</title>
<!-- Look up these hosts early. Nothing else happens yet. -->
<link rel="dns-prefetch" href="https://analytics.example.com">
<link rel="dns-prefetch" href="https://chat.example.com">
<link rel="stylesheet" href="/site.css">
</head>
Time the page waits for the connection
dns-prefetch saves at most the DNS part (60 ms). Preconnect can save the whole setup, if it has time to finish before the page needs the host.
This is a simplified model. It assumes the hint is found at 200 ms and that each step takes the time you set. Real numbers vary a lot with distance, network, and caching, so measure your own.
In the lab, compare the three bars. Notice how dns-prefetch only trims the DNS part of the wait, while preconnect can remove much more, if it has enough time to finish.
What Is DNS?β
Think of DNS as the phone book of the internet. You know a name, cdn.example.com, and the phone book gives you a number, such as 203.0.113.7. Only then can your browser "call" the server.
The lookup asks a chain of servers, and the answers are remembered for a while, by the browser, your device, and your internet provider. So a host you have used recently is often quick, and a host you have never used may take longer. The difference can be small (a few milliseconds) or large (hundreds, on a slow or distant network), and it varies a lot.
The Steps of a New Connectionβ
Fetching a file from a new server takes several steps before any data arrives. Each hint covers different steps.
| Step | What happens | dns-prefetch | preconnect |
|---|---|---|---|
| 1. DNS lookup | Find the server's address | Yes | Yes |
| 2. Connect | Open a line to the server | No | Yes |
| 3. Secure handshake | Agree on encryption (for https) | No | Yes |
| 4. Request | Ask for the file | No | No |
So dns-prefetch does one step of three. That is why it is so cheap, and also why it saves less. Next lesson, Preconnect, covers the full set.
How to Write Itβ
<link rel="dns-prefetch" href="https://cdn.example.com">
- Use the origin only: the scheme and host, such as
https://cdn.example.com. A path adds nothing, because DNS cares only about the host. - You do not need
crossorigin. A lookup is the same however the files are later fetched. - Add one line per host, and keep the list short.
When to Use Itβ
dns-prefetch is a good fit for hosts that are third-party (not your own site), and that the page may need later, but not at once:
- Analytics and tag managers
- Chat or support widgets
- Embedded videos or maps further down the page
- Social buttons
- Any host that comes up after a script runs
It does nothing for your own site, because the browser has already looked up the host that served the page.
Pairing It with Preconnectβ
A very common pattern puts both hints on the same host:
<link rel="preconnect" href="https://cdn.example.com" crossorigin>
<link rel="dns-prefetch" href="https://cdn.example.com">
Why both? Browsers that do not understand preconnect can still do the DNS lookup, and some browsers limit how many connections they will open early. The lookup acts as a safety net. Check MDN for current browser support of each.
Sort the Hostsβ
Here is a bakery page that uses seven hosts. For each one, choose the hint it deserves, or none.
A simple rule: needed straight away, so preconnect. Maybe needed later, so dns-prefetch. Your own site or unlikely, so nothing.
When It Does Not Helpβ
- Your own site. The lookup is already done.
- Hosts the page never uses. You pay a small cost for nothing.
- Hosts used much later. The answer is only remembered for a limited time, so a lookup that is far ahead of its use may be repeated.
- Many hosts. A long list adds noise, and each lookup uses a little battery and network on mobile.
- A visitor with a fast, warm DNS cache. The lookup may be nearly free anyway.
Because the cost is so low, a few unneeded dns-prefetch hints do little harm. But a careful list is still better than a long one.
Checking That It Workedβ
- Open DevTools, then Network, and reload.
- Click a request to a third-party host, and open the Timing tab.
- Look at DNS Lookup. With a good prefetch it may be zero, or not shown, because the lookup already finished.
- Compare with the same page without the hint. Remember that DNS caching at several levels can hide the difference on a second visit, so clear the cache or use a test tool that starts fresh, such as WebPageTest.
- Check that you have not hinted any host the page never uses.
Best Practicesβ
- Hint third-party hosts you may use, not your own.
- Use origins only.
- Pair it with
preconnectfor hosts that matter most. - Keep the list short.
- Remove hints when you stop using a service.
- Do not build hints from untrusted input, such as user-submitted links.
- Test on a fresh cache and a slow connection.
- Measure. A hint that does not move a number is clutter.
Quick Checklistβ
- Every hinted host is used by the page, or likely to be
- No hint for your own site
- Each hint is an origin, with no path
- Hosts needed immediately use
preconnect, withdns-prefetchas a fallback - No hosts that need consent are hinted before consent
- Old services have had their hints removed
- DevTools shows the DNS time dropped
Security and Privacy Notesβ
- A lookup tells the network something. When your page asks for a lookup, the visitor's DNS resolver (often their internet provider) learns that this page uses that host. A hint triggers the lookup earlier, and for hosts the visitor may never actually use. If you need consent before contacting certain third parties (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.
- Never create hints from untrusted input. If a page turns a user-submitted link into a
dns-prefetch, the visitor's browser looks up a host chosen by someone else, which can be used to track visitors. Generate hints only from a list you control. - Only hint hosts you trust. Even a lookup is a small way of reaching out.
- Encrypted DNS (offered by some browsers and providers) hides lookups from people on the network, but not from the resolver. It is the visitor's choice, not something your HTML controls.
Global Audience Notesβ
- DNS speed varies a lot by country, provider, and network. Visitors on slow or distant networks may see lookups of hundreds of milliseconds, so they gain most from an early lookup.
- Third-party hosts can be slow, or blocked, in some places. A page must still work if a host never answers, so never make the page depend on a hinted service.
- Mobile networks pay a price for every lookup, in time and battery. A short list is kind to them.
- Test from different regions, using a tool such as WebPageTest, since DNS behaviour differs around the world.
Common Mistakes to Avoidβ
- Hinting your own site.
- Including a path and thinking it matters.
- Hinting dozens of hosts.
- Hinting hosts the page never uses.
- Using
dns-prefetchfor a host needed straight away whenpreconnectwould save more. - Hinting before consent for services that need it.
- Building hints from user input.
- Leaving hints for services you stopped using.
- Adding
crossorigin, which is not needed for DNS lookups. - Assuming it always helps, without testing on a fresh cache.
Practice Timeβ
- In the Connection Lab, use the "Far or slow" preset. How much does
dns-prefetchsave? How much doespreconnectsave? - Set "the page first needs this host at" to 200 ms or less. Why does no hint help any more? Then slowly raise it, and watch each hint start to save time.
- Sort the hosts. Which one surprised you?
- List every third-party host on a page you built, using the Network panel.
- Decide which need
preconnect, which needdns-prefetch, and which need nothing. - Add the hints, and check the DNS Lookup time in the Timing tab.
- Remove a hint for a host the page no longer uses.
- Bonus: run WebPageTest on your page and look at the connection view.
Quick Recapβ
Click a question to check your answer.
1. What does dns-prefetch do?
It asks the browser to look up a host's address early, so that step is already done when the page needs the host. It does nothing else.
2. What does preconnect add?
It also opens the connection and finishes the security handshake, so more of the setup is done in advance. It costs a little more, because the connection stays open.
3. Which hosts deserve dns-prefetch?
Third-party hosts the page may need later, such as analytics, chat widgets, and embedded media. Not your own site.
4. Why pair dns-prefetch with preconnect?
It is a safety net: browsers that do not support preconnect, or that limit early connections, can still do the lookup.
5. Do you need crossorigin on dns-prefetch?
No. The lookup is the same whichever mode the files are fetched in. (preconnect is different, and may need it.)
6. What privacy risk does it carry?
It triggers lookups, which tell the visitor's DNS resolver about the hosts your page uses, and may contact services before consent. Never build hints from untrusted input.
Key Takeawaysβ
- DNS turns a host name into an address, and it takes time for each new host.
dns-prefetchdoes just that step, early. It is cheap, and saves less.- Use it for third-party hosts you may need, and
preconnectfor those you need at once. - Origins only, no
crossorigin. - Keep the list short, and remove hints you no longer need.
- Mind privacy: wait for consent where it is needed, and never hint untrusted hosts.
- Measure the DNS time in DevTools, on a fresh cache.
Next up: Preconnect, the fuller hint that opens the whole connection early.