How to Use Connection Preconnect Hints
Cut request latency with preconnect, dns-prefetch and preload hints, so the browser sets up connections and fetches resources before they are needed.
On this page
Introduction
The first request to a third-party origin pays for DNS resolution, TCP connection and TLS negotiation before any byte of the response arrives. A page that loads fonts, scripts and images from several origins pays that cost several times, serialised with rendering. Preconnect hints let the page tell the browser to set up those connections early, in parallel, so the cost is paid before the resource is needed. Used well, they shave the setup latency off the critical path; used carelessly, they open connections that are never used.
The Setup Cost
A request to a new origin needs a DNS lookup, a TCP handshake and, for HTTPS, a TLS handshake, all before the request is sent. On a cold connection that is several round trips, and if the browser does not know about the origin until it parses the HTML and finds the resource, the setup is serialised with resource discovery.
Hints move the setup earlier: the browser reads the hint in the HTML head and starts the work immediately, in parallel with other setup, so by the time the resource is needed, the connection is ready. The saving is the setup latency, which on mobile networks can be hundreds of milliseconds per origin.
<!-- in the document head -->
<!-- full setup (DNS + TCP + TLS) for a known origin used soon -->
<link rel="preconnect" href="https://fonts.example.com" crossorigin>
<!-- DNS only, for an origin you will connect to but not immediately -->
<link rel="dns-prefetch" href="https://analytics.example.com">
<!-- fetch a critical resource the browser would only discover later -->
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
Preconnect, DNS-Prefetch and Preload
preconnect does the full setup: DNS, TCP and TLS. It is the most expensive hint, saving the most, and should be reserved for origins you will use soon and on the critical path. dns-prefetch does only the DNS lookup, cheaper and useful for origins that may be used. preload fetches a specific resource so it is available when needed, reducing the discovery delay.
The three address different costs: DNS resolution, connection setup and resource fetch. Use each for its purpose: preconnect the critical third-party origin, dns-prefetch the ones that may be used but are not critical, and preload the specific files on the critical rendering path.
# hint -> cost paid early
preconnect -> DNS + TCP + TLS (expensive; for critical origins)
dns-prefetch -> DNS only (cheap; for likely origins)
preload -> fetch a resource (reduces discovery delay)
prefetch -> low-priority fetch for a future navigation
# pick by cost saved and how soon the origin or resource is needed
Preloading Critical Resources
preload is for resources the browser discovers late but needs early: a font referenced from CSS, a hero image referenced from a stylesheet, a script loaded by another script. Preload declares it in the head so the fetch starts at parse time, before the referencing file is processed.
Preload must be precise: the as attribute, and crossorigin for fonts, so the browser fetches the right way and reuses the preloaded resource rather than fetching it again. A preload with wrong attributes causes a double fetch, which is worse than no preload. Measure that the preloaded resource is actually reused.
<!-- preload a font used by CSS, with the exact attributes -->
<link rel="preload" as="font" type="font/woff2"
href="/fonts/inter.woff2" crossorigin>
<!-- preload a hero image referenced from a stylesheet -->
<link rel="preload" as="image" href="/hero.webp" fetchpriority="high">
<!-- wrong attrs => double fetch (worse than no preload); verify reuse in devtools -->
Choosing What to Hint
Hints consume resources, so use them selectively. Preconnect the origins on the critical path and used soon; do not preconnect an origin that might be used, because an unused preconnect wastes a connection and competes with the connections that matter. dns-prefetch is cheap enough for likely origins.
Base the decision on the audit: which origins appear in the critical path, which resources are discovered late but needed early, and which third-party origins are actually used. Adding hints blindly adds setup work that competes for bandwidth and sockets.
# decide from an audit
[ ] critical-path origins used soon -> preconnect
[ ] likely but non-critical origins -> dns-prefetch
[ ] late-discovered critical resources-> preload (exact as/crossorigin)
[ ] do NOT preconnect speculative origins (wastes a connection)
# hints compete for bandwidth and sockets; add deliberately
Measuring the Effect
Measure with the browser’s own tools: the network panel shows connection setup time and whether a preloaded resource was reused. A preconnect that saved setup time shows as a shorter connection time for that origin; a preload that worked shows no double fetch.
Field data matters more than a lab run: the saving is largest on high-latency mobile networks, which a fast lab machine hides. Use the performance API to record the setup time for hinted origins and compare hinted against unhinted in the field.
// Record connection timing for hinted origins in the field.
new PerformanceObserver((list) => {
for (const e of list.getEntries()) {
if (e.entryType === 'resource') {
const setupMs = e.connectEnd - e.startTime; // connection setup time
navigator.sendBeacon('/perf', JSON.stringify({ name: e.name, setupMs }));
}
}
}).observe({ type: 'resource', buffered: true });
// compare setup time for hinted vs unhinted origins on real networks
HTTP Headers and Early Hints
Hints can also come from HTTP headers, which is useful when the server knows the resources before the HTML is parsed: a Link header with rel=preconnect or rel=preload. This is especially useful for a redirecting request or an early response before the HTML body.
Early Hints (103 responses) extend this: the server sends Link headers before the final response, letting the browser start preconnects and preloads while the server is still preparing the main response. It is a way to hint before the HTML exists, cutting the setup from the critical path on slow server responses.
# Link headers carry the same hints from the server
HTTP/1.1 200 OK
Link: <https://fonts.example.com>; rel=preconnect; crossorigin
Link: </fonts/inter.woff2>; rel=preload; as=font; crossorigin
# Early Hints: send links BEFORE the final response
HTTP/1.1 103 Early Hints
Link: <https://cdn.example.com>; rel=preconnect
# browser starts setup while the server prepares the main response
Common Pitfalls and Conclusion
Do not preconnect speculative origins; an unused preconnect wastes a connection and competes for sockets. Do not preload with wrong attributes, which causes a double fetch. Do not hint too many origins, which spreads the bandwidth thin. And do not judge the effect on a fast lab machine; the saving is on high-latency real networks.
Preconnect, dns-prefetch and preload move connection setup and resource discovery off the critical path by telling the browser to start early. Preconnect the critical third-party origins, dns-prefetch the likely ones, preload the late-discovered critical resources with exact attributes, and measure the saving in the field. Used selectively, the hints shave the setup latency that accumulates across origins.
Verify in the field that hinted origins show a shorter connection setup time.
Sources: web.dev: Resource hints, MDN: rel=preconnect, RFC 8297: Early Hints.
FAQ
- Preconnect or dns-prefetch?
- Preconnect does the full setup (DNS, TCP, TLS) and saves the most, so reserve it for critical-path origins used soon. Dns-prefetch does only the DNS lookup and is cheap enough for origins you might use. A wasted preconnect costs more than a wasted dns-prefetch.
- Why did my preloaded font fetch twice?
- Because the preload attributes did not match how the font is requested, usually a missing crossorigin. The browser then treats the preloaded resource and the actual request as different and fetches twice. Match as, type and crossorigin exactly.
Report a problem with this articleOpens a pre-filled issue on GitHub.