function advagg_add_preload_header in Advanced CSS/JS Aggregation 7.2
Add in the preload 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.
2 calls to advagg_add_preload_header()
- advagg_add_resource_hints_array in ./
advagg.module - Find dns_prefetch and call advagg_add_dns_prefetch().
- _advagg_process_html in ./
advagg.module - Replacement for template_process_html().
File
- ./
advagg.module, line 6466 - Advanced CSS/JS aggregation module.
Code
function advagg_add_preload_header($url = '', $as = '') {
// Get defaults and setup static's.
$list =& drupal_static(__FUNCTION__ . ':list', array());
$output =& drupal_static(__FUNCTION__ . ':output');
$header_strlen =& drupal_static(__FUNCTION__ . ':strlen', 0);
static $resource_hints_preload_order;
if (!isset($resource_hints_preload_order)) {
$resource_hints_preload_order = advagg_get_resource_hints_preload_settings();
}
if (!isset($output)) {
$keys = array_keys($resource_hints_preload_order);
$output = array_fill_keys($keys, array());
}
// Output headers.
if (empty($url)) {
// Call hook_advagg_preload_header_alter()
drupal_alter('advagg_preload_header', $output);
// Build header string.
$header_value = '';
foreach ($output as $value) {
if (!empty($value)) {
// Remove empty values.
$value = array_filter($value);
foreach ($value as $string) {
$header_strlen += strlen($string) + 2;
// Don't add if over the limit.
if ($header_strlen >= variable_get('advagg_resource_hints_preload_max_size', ADVAGG_RESOURCE_HINTS_PRELOAD_MAX_SIZE)) {
continue;
}
// Add to $header_value string.
if (empty($header_value)) {
$header_value = $string;
}
else {
$header_value .= ',' . $string;
}
}
}
}
if (!empty($header_value)) {
drupal_add_http_header('Link', $header_value, TRUE);
}
return FALSE;
}
// Check for duplicates.
if (isset($list[$url])) {
return FALSE;
}
// Fill in missing info.
$payload = "<{$url}>; rel=preload";
list($as, $type, $crossorigin, $parse) = advagg_get_preload_info_from_url($url, $as);
if (!empty($as) && $as === 'php') {
$list[$url] = FALSE;
return FALSE;
}
$additional_info = array();
if (!empty($crossorigin)) {
$additional_info[] = "crossorigin";
}
if (!empty($type)) {
if ($type !== 'text/css' || $type !== 'text/javascript' || $as !== 'image') {
// Type is not needed for css/js files and images.
$additional_info[] = "type=\"{$type}\"";
}
}
$additional_info = implode('; ', $additional_info);
// Get settings.
if (!empty($as) && !empty($resource_hints_preload_order[$as])) {
$settings = $resource_hints_preload_order[$as];
}
elseif (!empty($resource_hints_preload_order['all_others'])) {
$settings = $resource_hints_preload_order['all_others'];
}
// Apply settings.
if (!$settings['enabled']) {
$list[$url] = FALSE;
return FALSE;
}
if (!$settings['local'] && empty($parse['host'])) {
$list[$url] = FALSE;
return FALSE;
}
if (!$settings['external'] && !empty($parse['host'])) {
$list[$url] = FALSE;
return FALSE;
}
// Add additional info and queue.
if (!empty($as)) {
$payload .= "; as={$as}";
}
if (!empty($additional_info)) {
$payload .= "; {$additional_info}";
}
if (!$settings['push']) {
$payload .= "; nopush";
}
$list[$url] = TRUE;
$output[$as][] = $payload;
}