How To Add DNS Prefetch Hints In WordPress + Why You Shouldn’t Need To

Dns prefetch wordpress

It’s rare you should need to use DNS prefetch, but why?

DNS prefetch establishes early connections to third-party domains. However, third-party code should be hosted locally or its JavaScript should be delayed. Since prefetch is irrelevant for locally hosted files (and it would be counterintuitive to prefetch something that’s already being delayed), there’s usually no need to add DNS prefetch hints to WordPress. Instead, you should look into hosting files locally (fonts, analytics, YouTube thumbnails…) and delaying JavaScript.

Some optimization plugins let you add DNS prefetch hints (such as Perfmatters), otherwise you can use the Pre* Party Resources Hints plugin or manually add this code to your header.php file:

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

 

1. Why Preconnect And Delaying JavaScript Is Faster

Preconnect should typically be done with CDN URLs (i.e. cdn.yourdomain.com). And if fonts aren’t hosted locally, then you should also preconnect them (usually fonts.gstatic.com). Most cache plugins automatically add preconnect hints, but check their documentation to be sure.

For most other third-party code, you should delay the JavaScript. WP Rocket does this automatically while other plugins (like FlyingPress or Perfmatters) require you to add URLs manually. You can even delay entire plugins that load below the fold, for example, consider delaying third-party comment plugins like wpDiscuz/social sharing plugins. This delays third-party requests loaded by those plugins so they won’t appear as errors in PageSpeed Insights.

For third-party code that can’t be hosted locally or delayed, that’s the only reason to use DNS prefetch. While DNS prefetch establishes earlier connections, it’s likely not as effective as preconnect or delaying, otherwise it would probably be a PageSpeed Insights recommendation.

Preconnect google fonts - litespeed cache
Most cache plugins add preconnect automatically to third-party fonts + CDN URLs
Perfmatters delay javascript
Most cache plugins, Perfmatters, and Flying Script can delay JavaScript

 

2. Find Third-Party Domains On Your Site

The first step is to learn which domains to prefetch. Open your “reduce impact of third-party code” report to see third-party domains loading on your site. After you’ve hosted files locally and preconnected any third-party fonts + CDN URLs, the “leftovers” are what you can prefetch.

Reduce impact of third party code wordpress 4

 

3. Add DNS Prefetch Manually

To add DNS prefetch manually, add the examples to your header.php file:

<link rel="dns-prefetch" href="https://maps.googleapis.com">
<link rel="dns-prefetch" href="https://maps.gstatic.com">
<link rel="dns-prefetch" href="https://ajax.googleapis.com">
<link rel="dns-prefetch" href="https://apis.google.com">
<link rel="dns-prefetch" href="https://google-analytics.com">
<link rel="dns-prefetch" href="https://www.google-analytics.com">
<link rel="dns-prefetch" href="https://ssl.google-analytics.com">
<link rel="dns-prefetch" href="https://ad.doubleclick.net">
<link rel="dns-prefetch" href="https://googleads.g.doubleclick.net">
<link rel="dns-prefetch" href="https://stats.g.doubleclick.net">
<link rel="dns-prefetch" href="https://cm.g.doubleclick.net">
<link rel="dns-prefetch" href="https://www.googletagmanager.com">
<link rel="dns-prefetch" href="https://www.googletagservices.com">
<link rel="dns-prefetch" href="https://adservice.google.com">
<link rel="dns-prefetch" href="https://pagead2.googlesyndication.com">
<link rel="dns-prefetch" href="https://tpc.googlesyndication.com">
<link rel="dns-prefetch" href="https://youtube.com">
<link rel="dns-prefetch" href="https://i.ytimg.com">
<link rel="dns-prefetch" href="https://api.pinterest.com">
<link rel="dns-prefetch" href="https://pixel.wp.com">
<link rel="dns-prefetch" href="https://connect.facebook.net">
<link rel="dns-prefetch" href="https://platform.twitter.com">
<link rel="dns-prefetch" href="https://syndication.twitter.com">
<link rel="dns-prefetch" href="https://platform.instagram.com">
<link rel="dns-prefetch" href="https://platform.linkedin.com">
<link rel="dns-prefetch" href="https://disqus.com">
<link rel="dns-prefetch" href="https://sitename.disqus.com">
<link rel="dns-prefetch" href="https://s7.addthis.com">
<link rel="dns-prefetch" href="https://w.sharethis.com">
<link rel="dns-prefetch" href="https://s1.wp.com">
<link rel="dns-prefetch" href="https://1.gravatar.com">
<link rel="dns-prefetch" href="https://s.gravatar.com">
<link rel="dns-prefetch" href="https://stats.wp.com">
<link rel="dns-prefetch" href="https://securepubads.g.doubleclick.net">
<link rel="dns-prefetch" href="https://ajax.microsoft.com">
<link rel="dns-prefetch" href="https://s3.amazonaws.com">
<link rel="dns-prefetch" href="https://a.opmnstr.com">
<link rel="dns-prefetch" href="https://script.hotjar.com">
<link rel="dns-prefetch" href="https://code.jquery.com">
<link rel="dns-prefetch" href="https://player.vimeo.com">
<link rel="dns-prefetch" href="https://github.githubassets.com">
<link rel="dns-prefetch" href="https://referrer.disqus.com">
<link rel="dns-prefetch" href="https://stats.buysellads.com">
<link rel="dns-prefetch" href="https://s3.buysellads.com">

 

4. Add DNS Prefetch With Plugins

Many cache plugins support DNS prefetch, but if it doesn’t, use Pre* Party Resource Hints.

WP Rocket’s DNS Prefetchprefetch DNS requests is found in the Preload settings.

Wp rocket prefetch dns requests

Perfmatters’ DNS Prefetch prefetch DNS requests is found in the Preloading settings.

Perfmatters dns prefetch 1

SiteGround Optimizer’s DNS Prefetch – DNS pre-fetch for external domains is found in the Frontend Optimizations settings.

Siteground optimizer prefetch external domains

LiteSpeed Cache’s DNS Prefetch – DNS prefetch and DNS prefetch control is in Page Optimizations → HTML settings (leave prefetch control on to prefetch URLs in the document).

Litespeed cache dns prefetch

 

5. Measure Effectiveness Of DNS Prefetch

Open your Waterfall chart (i.e. in GTmetrix) to see how long third-party domains take to load. Be sure to benchmark your results before & after adding DNS prefetch. Fortunately, there should be little to no penalty for prefetching third-party domains that aren’t loaded on your site (however, you have to be careful with this if using preload & preconnect). Hopefully, it’s slightly faster now.

Dns prefetch in waterfall chart

Drop me a comment if you need help, and don’t forget to preload key requests!

Cheers,
Tom

You Might Also Like: