Course contentsShow
← Web Development
Lesson 1962 of 2,98236. Performance Optimization TechniquesFree lesson

DNS Prefetch for Domain Resolution

Use dns-prefetch to resolve domain names early, reducing DNS lookup time for cross-origin resources.

What You'll Learn

You'll learn how to use the dns-prefetch resource hint to reduce latency by resolving domain names before resources are actually requested.

The Problem: DNS Lookup Delays

Every time your page needs a resource from a different domain (like a CDN, analytics script, or web font), the browser must first perform a DNS lookup to convert that domain name into an IP address. This lookup happens before the browser can even start downloading the resource, adding precious milliseconds (sometimes 20–120ms) to your loading time.

Think of DNS lookup like calling directory assistance before you can dial someone's phone number. It's a necessary step, but it takes time.

How DNS Prefetch Works

The dns-prefetch hint tells the browser: "I know we'll need resources from this domain soon, so go ahead and resolve its IP address now while you have idle time." The browser performs the DNS lookup in the background, so when you actually request a resource from that domain, the address is already cached.

Using DNS Prefetch

Add a <link> tag in your HTML <head>:

<link rel="dns-prefetch" href="//cdn.example.com">
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="dns-prefetch" href="//analytics.google.com">

Notice the // protocol-relative format—this works for both HTTP and HTTPS.

When to Use It

Use dns-prefetch for cross-origin domains you'll definitely use:

  • CDNs hosting your assets
  • Third-party analytics or social widgets
  • Web font providers
  • API endpoints

Avoid over-using it. Each prefetch consumes resources and bandwidth. Only prefetch domains you're confident will be used on the page.

Key Takeaway: dns-prefetch reduces latency by resolving domain names early, shaving milliseconds off resource load times for cross-origin requests with minimal overhead.