Long time since I’ve used it myself, but this code snippet (is supposed to) do(es) a light form of minification for inline CSS/ JS;
function minify_inline_code($htmlIn) {
preg_match_all('#<(style|script).*>(.*)</(style|script)>#Usmi',$htmlIn,$code);
foreach ($code[0] as $codeIn) {
$repl["in"] = $codeIn;
$repl["out"] = str_replace(array("\r\n", "\n", "\r", "\t"),"",$codeIn);
$replArr[] = $repl;
}
foreach ($replArr as $replNow) {
$htmlIn = str_replace($replNow["in"],$replNow["out"],$htmlIn);
}
return $htmlIn;
}
add_filter("autoptimize_html_after_minify","minify_inline_code");
re. technical question; AO indeed runs on every request that reaches WordPress and as such does have a light impact on TTFB. for that reason it is advised to combine AO with a proper page caching solution (caching plugin such as WP Super Cache or server-level cache such as Nginx caching or 3rd party such as Cloudflare’s page rules).
hope this helps,
frank
Thread Starter
apedog
(@apedog)
Thanks,
I can use that hook. Simple enough. Tho I sincerely think this could be a welcome addition to AO’s out-of-the-box settings.
Thank you also for the technical info. I installed AO for use with wp-super-cache. Will have to Query Monitor it see if I should turn it off when serving non-static pages.
I can use that hook. Simple enough. Tho I sincerely think this could be a welcome addition to AO’s out-of-the-box settings.
might be, one day, but in general the performance impact will close to non-existing (except if you have tons of very un-optimized inline CSS/JS).
Will have to Query Monitor it see if I should turn it off when serving non-static pages.
based on tests I ran on a servebolt test-site, the impact on TTFB is 30-50ms (assuming a warmed up AO cache), but this is compensated by significantly better first paint & onLoad times 🙂
Thread Starter
apedog
(@apedog)
It’s not about impact to performance. It’s about wanting to keep inline css and javascript inline. This is a very common use-case. Some scripts don’t play well when moved outside of the header. Just optimizing the existing html output – without changing the structure.
Thanks for the heads up about the performance imapct 🙂