Drupal and DNS Prefetch

In a bid to help you gain performance from your web apps, a new feature introduced with HTML5 is the ability to prefetch DNS resources.  Currently widely supported (Firefox 3.5+, Chrome, Safari 5+ and IE 9+), prefetching is where your browser will in the background to a DNS lookup for the addresses that you give. This means when it comes to access a resource at a host, it can skip the DNS lookup, thus saving precious milliseconds on your load time.

This is one way you can implement prefetching with Drupal, inside a THEME_prerpocess_page() hook.

function MYTHEME_preprocess_page(&$variables, $hook) {
    drupal_add_html_head(array(
    '#tag' => 'link',
    '#attributes' => array(
      'rel' => 'dns-prefetch',
      'href' => '//www.googletagmanager.com',
    ),
  ), 'googletagmanager_dns_prefetch');
}

This will produce the following html in the head of your site

<link rel="dns-prefetch" href="//www.googletagmanager.com" />

Posted

in

,

by

Comments

Leave a Reply