html-compressor icon indicating copy to clipboard operation
html-compressor copied to clipboard

Head inline script converted into external JS file

Open cristi-contiu opened this issue 8 years ago • 6 comments

EXPLANATION OF THE ISSUE

I have noticed a small issue with the compress_combine_head_js option ( htmlc_compress_combine_head_js in WP Admin, as I use this library via your excellent CometCache plugin): When there is an inline script present in the head, it will convert it to an external JS file which requires an render-blocking HTTP request.

STEPS TO REPRODUCE THE ISSUE

Add a inline script to HEAD section, for example <script> var wp_ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>'; </script>

BEHAVIOR THAT I EXPECTED

I would expect the HEAD external JS (scripts with "src") to be combined/compressed/minified, but the inline scripts (like to one above) to be ignored (maybe minified inline, via different option).

BEHAVIOR THAT I OBSERVED

The above script will be converted to: <script type="text/javascript" src="http://example.com/wp-content/cache/zencache/htmlc/public/example-com/f/f/d/0/b/b4d1c8d399d0056c01f51915b7df5a3d-compressor-part.js?iv=1"></script>

Converting the (usually small) inline JS script to an external JS file which requires an extra HTTP request to download totally overrides any performance benefit.

cristi-contiu avatar Aug 10 '17 05:08 cristi-contiu

@cristi-contiu I noticed the path you posted above contains zencache—are you running the latest version of Comet Cache Pro?

raamdev avatar Aug 19 '17 00:08 raamdev

I have the same issue. This is a real problem because it compress code like this:

  t.src=v;s=b.getElementsByTagName(e)[0];
  s.parentNode.insertBefore(t,s)}(window, document,'script',
  'https://connect.facebook.net/en_US/fbevents.js');

playinteractive avatar Nov 21 '17 23:11 playinteractive

@cristi-contiu I'll see what we can do to improve the defaults, but you can tune this for your site by adding exclusions. See: Dashboard → Comet Cache → Plugin Options → HTML Compressor → JavaScript Exclusion Patterns

In your case, add this line to the configuration if you want to exclude that snippet.

var wp_ajax_url

jaswrks avatar Nov 24 '17 13:11 jaswrks

@playinteractive In your case, add this line:

connect.facebook.net/en_US/fbevents.js

jaswrks avatar Nov 24 '17 13:11 jaswrks

Thanks man, I'll try it.

playinteractive avatar Nov 27 '17 18:11 playinteractive

@jaswrks Thank you for the tip!

cristi-contiu avatar Nov 28 '17 10:11 cristi-contiu