You are here

function cdn_html_head_alter in CDN 7.2

Implements hook_html_head_alter().

Adding dns-prefetch <link> elements via hook instead of drupal_add_html_head to place them as early in the head as possible so that the browser acts upon them ASAP.

See also

https://github.com/h5bp/html5-boilerplate/blob/master/doc/extend.md

File

./cdn.module, line 498

Code

function cdn_html_head_alter(&$head_elements) {
  if (!cdn_status_is_enabled()) {
    return;
  }
  $domains = cdn_get_domains();

  // Two sets of markups: because IE9 interprets rel="prefetch" as "dns-prefetch".
  // IE10 intreprets both dns-prefetch and prefetch as DNS pre-resolve.
  $markup = null;
  $ie_markup = null;
  if (count($domains)) {

    // Enable prefetching.
    $head_elements['cdn_dns_prefetch_meta'] = array(
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
        'http-equiv' => 'x-dns-prefetch-control',
        'content' => 'on',
      ),
      // System meta for content-type is at weight -1000. We'll insert the DNS tags
      // shortly after that.
      '#weight' => -900.9999,
    );

    // The domain names to prefetch. Use protocol-relative URLs.
    foreach ($domains as $domain) {
      $element = array(
        '#tag' => 'link',
        '#attributes' => array(
          'rel' => 'dns-prefetch',
          'href' => '//' . $domain,
        ),
      );
      $link_el = theme('html_tag', array(
        'element' => $element,
      ));
      $markup .= $link_el;
      $ie_markup .= preg_replace('/rel="dns-prefetch"/', 'rel="prefetch"', $link_el);
    }
    $markup .= '<!--[if IE 9]>' . PHP_EOL;
    $markup .= $ie_markup;
    $markup .= '<![endif]-->' . PHP_EOL;
    $head_elements['cdn_dns_prefetch_block'] = array(
      '#type' => 'markup',
      '#markup' => $markup,
      '#weight' => -900,
    );
  }
}