Photo by Cytonn Photography on Unsplash
Resource Hints: Preconnect vs DNS Prefetch
Modern web pages load resources from multiple origins: fonts from CDN providers, analytics scripts, third-party APIs, and embedded content. Each new origin requires a full connection setup before any bytes can flow. Resource hints let you tell the browser to prepare these connections early, eliminating hundreds of milliseconds of idle waiting time.
The connection setup tax
When a browser first connects to an origin, it performs three sequential steps: DNS resolution to turn the domain into an IP address, a TCP handshake to establish a connection, and if HTTPS is involved, a TLS handshake to negotiate encryption. On a mobile network with 100ms round-trip latency, these three steps cost around 300ms before a single byte of the actual resource arrives. Multiply that across every third-party domain, and you have a substantial performance penalty hidden in connection establishment.
The parser discovers these dependencies late. Your HTML references a stylesheet, which references a web font hosted on a CDN. By the time the browser knows it needs that font, the opportunity to establish the connection in parallel with other work has passed. The font request blocks on connection setup that could have happened earlier.
DNS prefetch: resolve early
The dns-prefetch hint tells the browser to resolve a domain name to an IP address before any resource from that origin is actually requested:
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
This eliminates DNS lookup latency, typically 20–120ms depending on resolver cache state and network conditions. The browser performs the lookup in the background, caching the result so that when a resource from that origin is later discovered, the DNS step is already complete.
DNS prefetch is lightweight. It only performs the lookup and stores the result; it doesn’t open a TCP connection or perform a TLS handshake. This makes it appropriate even for origins that might not be used on every page load, since the cost of a wasted DNS lookup is minimal compared to the cost of a full connection.
Preconnect: full connection warmup
The preconnect hint goes further, instructing the browser to perform the complete connection setup:
<link rel="preconnect" href="https://fonts.gstatic.com">
This eliminates all three phases—DNS, TCP, and TLS—so that when the actual resource request occurs, the connection is ready and data can flow immediately. On high-latency networks, this saves 200–400ms per origin.
Preconnect is more expensive than DNS prefetch because it consumes an actual TCP socket and holds connection state. Each preconnected origin ties up system resources. Browsers typically limit the number of simultaneous preconnect hints they’ll honor, often to around six. If you preconnect to an origin and never use it, you’ve wasted CPU, memory, and potentially network bandwidth on the TLS handshake.
Choosing between them
Use preconnect when you know with high confidence that the page will request a resource from that origin. Critical third-party resources like fonts used above the fold, analytics scripts that run on every page, or API endpoints required for initial render are good candidates.
Use dns-prefetch as a fallback for origins that might be used, or when you’ve already used your preconnect budget. It’s less aggressive and still provides value. Some developers use both, since browsers that don’t support preconnect will fall back to dns-prefetch.
The browser ignores hints for same-origin resources, since the connection to your own domain is already established by the time it parses your HTML. Focus hints on cross-origin dependencies.
Practical constraints
Preconnect hints expire. If the browser completes the connection setup but no resource request arrives within about ten seconds, it closes the connection to free resources. The timing varies by browser, but the intent is to prevent indefinite resource consumption from unused hints.
Connection coalescing in HTTP/2 and HTTP/3 means that multiple domains resolving to the same IP and sharing a certificate can reuse a single connection. This reduces the benefit of multiple preconnect hints targeting related origins, though the DNS resolution cost still applies per domain.
Hints don’t override the browser’s connection limit. If your page is already saturating the per-origin connection limit with active requests, a preconnect won’t add yet another connection beyond the browser’s policy. It just ensures that when capacity becomes available, the setup is done.
Resource hints are declarative performance tools. They let you encode knowledge about your page’s dependency graph directly in markup, shifting connection establishment from blocking critical path latency to parallelizable background work. When applied to high-latency third-party origins that matter for initial render, the time saved is measurable in user experience.