Featured image of post Speeding Up assets Loading using DNS-Prefetch and Preconnect Featured image of post Speeding Up assets Loading using DNS-Prefetch and Preconnect

Speeding Up assets Loading using DNS-Prefetch and Preconnect

Reduce connection overhead by introducing dns-prefetch and preconnect resource hints inside HTML headers.

Introduction

Every external resource — fonts, scripts, images, API endpoints — requires a network connection. The overhead of DNS resolution, TCP handshake, and TLS negotiation can add hundreds of milliseconds to page load time. Resource hints like dns-prefetch and preconnect let you tell the browser to perform these steps in advance, before the resource is actually needed.


The Cost of Connection

Establishing an HTTPS connection involves multiple round trips:

StepLatency (approx.)
DNS lookup20-120 ms
TCP handshake1 RTT
TLS negotiation2 RTT
Total3+ RTT

On a 3G connection (300ms RTT), that’s nearly 1 second of overhead before any data is transferred. Resource hints eliminate most of this.


dns-prefetch

dns-prefetch resolves a domain name ahead of time:

<link rel="dns-prefetch" href="https://fonts.googleapis.com">

This initiates the DNS lookup for the domain while the browser is still parsing the HTML. By the time the browser encounters a resource from that origin, the IP address is already cached.

When to use: Simply referencing external domains. It’s lightweight — only DNS resolution, no connections opened.


preconnect

preconnect goes further: it performs DNS lookup + TCP handshake + TLS negotiation:

<link rel="preconnect" href="https://api.example.com" crossorigin>

The crossorigin attribute is required for resources loaded with CORS mode (fonts, fetch requests, etc.).

When to use: Critical third-party origins that the page needs as soon as possible — web font providers, analytics endpoints, CDNs.


Preconnect vs. Prefetch / Preload

These hints serve different purposes:

HintActionUse Case
dns-prefetchResolves DNS onlyLow priority origins
preconnectDNS + TCP + TLSCritical third-party origins
prefetchDownloads + caches resourceFuture navigation
preloadDownloads current-page resourceCritical assets for current page

Think of the hierarchy: dns-prefetch is cheapest, preconnect costs more but saves more, preload actually downloads the resource.


Practical HTML Examples

Web Fonts (most common use case)

<head>
  <link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="dns-prefetch" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
</head>

Third-Party APIs

<link rel="preconnect" href="https://api.example.com" crossorigin>

Analytics and CDN

<link rel="dns-prefetch" href="https://www.googletagmanager.com">
<link rel="preconnect" href="https://cdn.example.com" crossorigin>

Cloudflare and TTFB Considerations

If you use Cloudflare or similar CDN, resource hints are still valuable. Even though Cloudflare provides a fast edge, the first connection from a new user still requires DNS and TLS. Adding preconnect reduces TTFB (Time to First Byte) by eliminating connection setup time.

To measure impact:

<!-- Before optimization -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">

<!-- After optimization -->
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">

Measuring Impact with Lighthouse

Lighthouse audits for resource hints under “Preconnect to required origins”. Open Chrome DevTools → Lighthouse → Run audit. Look for:

  • Opportunity: Preconnect to required origins
  • Estimated savings: Up to several hundred milliseconds

You can also manually test using the Network panel with “Disable cache” enabled:

// Before vs. after — check connection timing
// Target: < 100ms connection time for preconnected origins

Best Practices

  1. Limit preconnect to 3-6 origins — each one opens a real socket
  2. Always add crossorigin for fonts and CORS resources
  3. Don’t preconnect to your own origin — the browser already does this
  4. Layer dns-prefetch as a fallback for browsers that don’t support preconnect
  5. Audit regularly with Lighthouse after site changes
<!-- Fallback pattern: both hints for wide support -->
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>

Conclusion

dns-prefetch and preconnect are low-effort, high-impact optimizations that reduce connection overhead for external resources. Use dns-prefetch as a lightweight hint for many origins, and reserve preconnect for the 3-6 most critical third-party origins. Combined with preload and proper caching, these hints can shave hundreds of milliseconds off your page load time.