Add in the dns-prefetch header for CSS and JS external files.

Parameters

string $url: The url of the external host.

Return value

bool TRUE if it was added to the head.

3 calls to advagg_add_dns_prefetch()
advagg_add_resource_hints_array in ./advagg.module
Find dns_prefetch and call advagg_add_dns_prefetch().
advagg_mod_page_alter in advagg_mod/advagg_mod.module
Implements hook_page_alter().
advagg_mod_xpath_script_external_dns in advagg_mod/advagg_mod.module
Use DOMDocument's loadHTML along with DOMXPath's query to find script tags.

File

./advagg.module, line 2598

Code

function advagg_add_dns_prefetch($url) {
    // Keep the order.
    $advagg_resource_hints_location = variable_get('advagg_resource_hints_location', ADVAGG_RESOURCE_HINTS_LOCATION);
    static $weight = -1001;
    if ($advagg_resource_hints_location == 3) {
        $weight = -999.9;
    }
    $weight += 0.0001;
    // Get the host.
    $parse = @parse_url($url);
    if (empty($parse['host'])) {
        // If just the hostname was given, build proper url.
        if (strpos($url, '.') && strpos($url, '/') === FALSE) {
            $parse['scheme'] = '//';
            $parse['host'] = $url;
            // Check for fragment.
            $pos = strpos($url, '#');
            if ($pos !== FALSE) {
                $parse['fragment'] = substr($url, $pos + 1);
                $parse['host'] = substr($url, 0, $pos);
            }
            // Put it back together and parse again.
            $url = advagg_glue_url($parse);
            $parse = @parse_url($url);
        }
        if (empty($parse['host'])) {
            return FALSE;
        }
    }
    // Filter out wrong schemes.
    if (!empty($parse['scheme']) && $parse['scheme'] !== 'http' && $parse['scheme'] !== 'https') {
        return FALSE;
    }
    // Filter out local host.
    $host = @parse_url($GLOBALS['base_root'], PHP_URL_HOST);
    if ($parse['host'] === $host) {
        return FALSE;
    }
    // Add DNS information for more domains.
    if (strpos($parse['host'], 'fonts.googleapis.com') !== FALSE) {
        // Add fonts.gstatic.com when fonts.googleapis.com is added.
        advagg_add_dns_prefetch('https://fonts.gstatic.com/#crossorigin');
    }
    // Build render array.
    if (variable_get('advagg_resource_hints_dns_prefetch', ADVAGG_RESOURCE_HINTS_DNS_PREFETCH)) {
        $element = array(
            '#type' => 'html_tag',
            '#tag' => 'link',
            '#attributes' => array(
                'rel' => 'dns-prefetch',
                'href' => '//' . $parse['host'],
            ),
            '#weight' => $weight,
        );
        // Add markup for dns-prefetch to html_head.
        drupal_add_html_head($element, 'advagg_resource_hints_dns_prefetch:' . $parse['host']);
    }
    if (variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT)) {
        // HTTPS use Protocol Relative; HTTP and scheme defined use given scheme.
        $href = '//' . $parse['host'];
        if (!$GLOBALS['is_https'] && isset($parse['scheme'])) {
            $href = "{$parse['scheme']}://{$parse['host']}";
        }
        $element = array(
            '#type' => 'html_tag',
            '#tag' => 'link',
            '#attributes' => array(
                'rel' => 'preconnect',
                'href' => $href,
            ),
            '#weight' => $weight,
        );
        if (!empty($parse['fragment']) && $parse['fragment'] === 'crossorigin') {
            $element['#attributes']['crossorigin'] = '';
        }
        // Add markup for dns-prefetch to html_head.
        drupal_add_html_head($element, 'advagg_resource_hints_preconnect:' . $parse['host']);
    }
    // Build render array. Goes after charset tag.
    if (!empty($parse['fragment']) && $parse['fragment'] === 'prefetch') {
        // Hacky way to open up a connection to the remote host.
        $element = array(
            '#type' => 'html_tag',
            '#tag' => 'link',
            '#attributes' => array(
                'rel' => 'prefetch',
                'href' => '//' . $parse['host'] . '/robots.txt',
            ),
            '#weight' => $weight,
        );
        drupal_add_html_head($element, 'advagg_prefetch:' . $parse['host']);
    }
    return TRUE;
}