filter for single files (excluded css)
-
Hi – In order to minimize renderblocking i add “preload” for the aggregated css link:
add_filter('autoptimize_filter_css_bodyreplacementpayload', 'preload_css_link');
function preload_css_link($bodyreplacementpayload){
$do_css = get_option('autoptimize_css');
$inline_css = get_option('autoptimize_css_inline');
if (isset($do_css) && !empty($do_css) && empty($inline_css)){
$string = $bodyreplacementpayload;
$url = $string;
preg_match("/href=\"(.*?)\"/i", $url, $matches);
//$matches ist array und key 1 enthält die url
$preload = '<link rel="preload" href="'.$matches[1].'" as="style" >';
$addpreload = $preload . $string;
return $addpreload;
}
}Add the top of the
<head>i get:<link rel="preload" href="https://xxx/wp-content/cache/autoptimize/css/autoptimize_9729dc67dd19461ac5811eef79a1e25f.css" as="style">
<link media="all" href="https://xxx/wp-content/cache/autoptimize/css/autoptimize_9729dc67dd19461ac5811eef79a1e25f.css" rel="stylesheet">Definitly an improvement. Renderblocking is zero. (tested on webpagetest.org: Zero render-blocking CSS files found.)
Now, i have some excluded css files. Deferring these files is no option, for the onload attribute
is rejected by csp (i added nonces to the aggregated js files). To enqueue them in the footer (get_footer) is also not a good solution.
I wonder, if there is a filter (similar to one shown above), that lets me alter the link of excluded single css files and allows to add a link with preload?
Thanks – theo
The topic ‘filter for single files (excluded css)’ is closed to new replies.