Title: Feature request: minify inline js without aggregating
Last modified: August 20, 2019

---

# Feature request: minify inline js without aggregating

 *  Resolved [apedog](https://wordpress.org/support/users/apedog/)
 * (@apedog)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/)
 * 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](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/#post-11852918)
 * 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](https://wordpress.org/support/users/apedog/)
 * (@apedog)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/#post-11853228)
 * 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](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/#post-11853826)
 * > 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](https://wordpress.org/support/users/apedog/)
 * (@apedog)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/#post-11877659)
 * 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](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/#post-11878444)
 * you’re welcome apedog, feel free to [leave a review of the plugin and support here](https://wordpress.org/support/plugin/autoptimize/reviews/#new-post)!
   🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Feature request: minify inline js without aggregating’ is closed to new
replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/feature-request-minify-inline-js-without-aggregating/#post-11878444)
 * Status: resolved