• Resolved apedog

    (@apedog)


    Hi,

    Would it be possible to have an option to minify inline js but keep it in the html head?
    My scripts don’t change, but I use inline wp_localize_script to have page-specific variables available to these scripts. So I dont’ want to aggregate these but it would be nice to have them compressed like the html.

    Also,
    On an unrelated note: Could you explain how the plugin works technically? Does it run and compress the html output on every page-load/request? Or does it keep a static cache copy of the html somewhere and simply bypass the page generation process? If it’s the former (compress html at run-time) what is the performance penalty? processing time vs. smaller payload. Benchmarks etc. Any information would be useful.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    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.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    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 ??

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    you’re welcome apedog, feel free to leave a review of the plugin and support here! ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Feature request: minify inline js without aggregating’ is closed to new replies.