Open Zodiac1978 opened 1 year ago
This is very interesting, probably it is something that cache plugin can do as the problem is that they change all the urls when they minify stuff so is better something php side.
I already use this trick for google fonts to avoid this kind of errors:
<link rel='preload' as='style' onload="this.rel='stylesheet' id='google-fonts-css' href='//fonts.googleapis.com/css?family=Raleway%3A400%2C700%2C900&font-display=swap&ver=6.1.1' type='text/css' media='all' />
with this php code:
add_filter('style_loader_tag', 'preload_gfont', 10, 2);
function preload_gfont($html, $handle) {
if ( $handle === 'google-fonts' ) {
return str_replace("rel='stylesheet'",
"rel='preload' as='style' onload=\"this.rel='stylesheet'\"", $html);
}
return $html;
}
Doc: https://stackoverflow.com/questions/32759272/how-to-load-css-asynchronously
Basically is saying to the browser to download it faster and after downloading saying this is a css so you can parse it (preload alone seems that is not enough).
Another thing is that on https://github.com/WordPress/performance/pull/617 there is a similar implementation just for the first image in the page.
Feature Description
Some time ago @stoyan shared this post on Twitter: https://www.phpied.com/faster-wordpress-rendering-with-3-lines-of-configuration/
After examining many client site, it seems to me that if the block editor is used, its CSS file is mostly (always?) a render-blocking CSS file.
He was first using the
.htaccess
andH2PushResource
(sending a 103 status code), but later added a PHP/WordPress solution using thesend_headers
action hook.He was unsure about the hard-coded version number, so I build this version of it:
See: https://gist.github.com/Zodiac1978/f58848785f4b86e4cd201ea35c2bf9da
Another way could be to use the
wp_preload_resources
filter to add this via markup (instead of headers):<link rel="preload" href="/styles/other.css" as="style">
Maybe this could be added automatically if the server is supporting http/2 (not sure if this check is necessary) and the block editor is in use.