You are here

function advagg_mod_prefetch_link in Advanced CSS/JS Aggregation 7.2

Have the browser prefech this domain to open the connection.

Parameters

array $js: JS array.

1 call to advagg_mod_prefetch_link()
advagg_mod_js_post_alter in advagg_mod/advagg_mod.module
Alter the js array.

File

advagg_mod/advagg_mod.module, line 1988
Advanced aggregation modifier module.

Code

function advagg_mod_prefetch_link(array &$js) {
  if (!variable_get('advagg_mod_prefetch', ADVAGG_MOD_PREFETCH)) {
    return;
  }
  foreach ($js as &$values) {
    if (!isset($values['dns_prefetch'])) {
      continue;
    }
    foreach ($values['dns_prefetch'] as &$url) {

      // Prefetch stats.g.doubleclick.net domain.
      if (strpos($url, '//stats.g.doubleclick.net') === FALSE) {
        continue;
      }
      if (variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT)) {
        $parse = @parse_url($url);
        $inline_script = 'var preconnect_support = false; try {if (document.createElement("link").relList.supports("preconnect")) {preconnect_support = true;}} catch (e) {} if (!preconnect_support) { var prefetch = document.createElement("link"); prefetch.href = "https://' . $parse['host'] . '/robots.txt"; prefetch.rel="prefetch"; document.getElementsByTagName("head")[0].appendChild(prefetch);}';
        $js['advagg_preconnect_support'] = array(
          'type' => 'inline',
          'group' => JS_LIBRARY - 1,
          'weight' => -50000,
          'scope_lock' => TRUE,
          'movable' => FALSE,
          'no_defer' => TRUE,
          'data' => $inline_script,
        ) + drupal_js_defaults($inline_script);
      }
      else {
        $url .= '#prefetch';
      }
    }
  }
}