Frontend Performance/Loading Less, Sooner
Lesson 13 of 20 · Episode 13

Resource Hints — dns-prefetch & preconnect

Warm up cross-origin connections before you need them so the DNS/TCP/TLS cost is already paid.

dns-prefetchpreconnectConnection
Watch on YouTube ↗

Resource hints are the best effort-to-payoff trade in performance: a single line of HTML that makes the browser smarter about when to do work. This lesson covers the two that warm up connections — dns-prefetch and preconnect — so a cross-origin resource isn't stuck doing handshakes on the critical path.

The category

Hints, not commands

The browser's preload scanner is already a good optimizer, but it can't read your mind. A resource hint is you telling it “this origin/resource matters — get a head start.” You add them as <link> tags (or HTTP headers), and they're cheap: minimal code, big potential win on the right page.

See it

Warming the connection

Connecting to a new origin isn't one step — it's DNS → TCP → TLS, and that can easily be 150 ms+ before a single byte of the resource transfers. Both hints move some of that work into idle time before the resource is discovered. Toggle between them:

InteractiveNo hint vs dns-prefetch vs preconnect
idle time (before the resource is discovered)
resource needed
ready 1240ms
No hint: every connection step waits until the resource is discovered, all on the critical path. Ready at 1240ms.
dns-prefetch warms just the DNS lookup; preconnect warms DNS + TCP + TLS. The warmed steps happen during idle time, off the critical path.
The cheap one

dns-prefetch

dns-prefetch resolves a domain name to an IP ahead of time and caches it. On its own it only saves the DNS step (often modest) — but its superpower is that it's supported everywhere, even ancient browsers, with almost zero overhead. It shines when you talk to many third-party origins.

resolve third-party domains early
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
<link rel="dns-prefetch" href="https://cdn.example.com">
The powerful one

preconnect

preconnect goes further: DNS and the TCP handshake and the TLS negotiation — the whole connection, ready to go. That's the big saving (the demo shows the request starting immediately on discovery). But opening a connection is expensive, so use it sparingly:

Two rules for preconnect
Only the critical few. Preconnecting to a dozen origins wastes resources — pick the 1–2 that gate your render (e.g. your font host). Only what you'll use soon. The browser drops idle connections after ~10 s, so preconnecting something you won't touch for a while just pays the cost for nothing.
preconnect to a critical cross-origin (note crossorigin for fonts)
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

That crossorigin attribute matters: font files are fetched in CORS mode, so the preconnect must match or the browser opens a second connection and you've gained nothing.

Belt and suspenders

Using them together

A common pattern is to list both: preconnect for modern browsers, with dns-prefetch as a fallback. If preconnect is supported, the DNS is already done and the dns-prefetch line is harmless; if it isn't (very old browsers), you still save the DNS lookup.

dns-prefetchpreconnect
WarmsDNS lookup onlyDNS + TCP + TLS
SavingSmall (~DNS time)Large (full connection)
Cost / overheadTinyReal — use sparingly
SupportEverywhere (even IE)Modern browsers
Use forMany third-party originsThe 1–2 most critical origins
Q1Multiple choice
What does preconnect do that dns-prefetch doesn't?
Q2Multiple choice
Why should you preconnect to only a couple of origins?
Q3Multiple choice
You preconnect to a font host but the fonts load twice. Likely cause?
Key takeaways
  • Resource hints are cheap <link> tags that tell the browser to start work early.
  • dns-prefetch warms just DNS — tiny, universal, great for many third parties.
  • preconnect warms DNS + TCP + TLS — big saving, but use it for only the 1–2 critical origins.
  • Add crossorigin when the resource (e.g. a font) is fetched in CORS mode.
  • List both as a fallback: preconnect for modern browsers, dns-prefetch everywhere else.
← Previous
12. Action on Visibility
Next →
14. Resource Hints — preload