WpW: Minification, Combination, and HTTP/2 Push

Written by

in

Welcome to another installment of WordPress Wednesday!

We’ve got more new features to talk about, so fasten your seatbelt and let’s get right to it: Minification, Combination, and HTTP/2 Push!

Presumably, you are already enjoying the benefits of caching with LiteSpeed Cache for WordPress. These three new optimization functions are designed to give your website an additional performance boost beyond that!

It’s generally accepted that the fewer plugins you have running the better, both in terms of performance, and in terms of just being able to manage everything efficiently. So, if you can replace two plugins with one that does everything, shouldn’t you?

These three new optimization functions (along with other new additions: database optimization, content delivery network support, and more coming soon) will allow you to retire your other optimization plugins and let LiteSpeed handle all of it.

For those of you who maybe have never minified or combined files before, let’s talk about what each of the new functions does. And then, we’ll show you how to set it all up.

What is Minification?

JavaScript, CSS, and HTML can all be put through the minification process.

When code is minified, all unnecessary whitespace characters, newline characters, and comments are removed. Minification, as its name implies, shrinks the size of the source code, and the resulting new code is more efficient to transmit over networks.

Minified code is distinctly difficult for a human to read, because all of those non-essential characters that were removed are precisely what made the original code readable in the first place. Despite any readability issues, minification is still popular because it’s generally considered more important to have a fast site than it is to have code your visitors can read. In fact, some might consider it a benefit to have their code be obfuscated to outsiders in this way.

Here’s an example of CSS before and after minification:

.sample {

border: 1px solid;

color: #123456;

/* this is a comment */

}

.sample{border:1px solid;color:#123456}

As you can see, the first block of code is easier on the eyes, but the second block of code takes up much less space, and still has the exact same functionality. This is just a tiny example. Imagine the space savings on a CSS file with thousands of styles defined!

With LSCWP minification, your source JS or CSS is not changed. We place the minified source into a new file, and the generated HTML includes that new file in the header instead of the original.

Minified CSS and JS will be placed in the code with a line that looks similar to:

<script data-minified='1' src='http://yoursite.com/min/3fb36.js'></script>

Please be aware, if you do not have pretty permalinks enabled (i.e., your site has URLs that look like http://yoursite.com/?p=123) then your minified code will have a ? in the URL like so:

<script data-minified='1' src='http://yoursite.com/?/min/3fb36.js'></script>

When HTML minification is enabled, your source HTML is also not changed. In fact, it can’t be changed because there is no source HTML. HTML is generated on-the-spot by WordPress and PHP, so if minification is enabled, the HTML will be generated already in the minified form.

What is Combination?

When a web app includes several JavaScript files, those files may be combined into one. The same goes for CSS files. This reduces the number of requests made by the browser and potentially removes duplicate code, both of which translate to improvements in speed.

With LSCWP combination, your source JS and CSS is not changed. We create a new file, the generated HTML includes that new file in the header, and the original files are not referenced.

One thing to note when combining JavaScript files: If you have JS code within the HTML itself, we cannot combine all of the JS without running the risk of introducing conflicts. If there is JS within your HTML, that JS will not be combined. All other JS will be combined, but it will not be placed in the header in this case. It will be placed in the footer instead.

What is HTTP/2 Push?

In an effort to reduce the number of requests necessary when loading a web page, HTTP/2 Push may be used to anticipate the browser’s needs and act upon them.

For instance, if the browser requests index.html, HTTP/2 can reasonably assume that the browser also wants the included CSS and JS files, and will push them along with index.html. The browser doesn’t need to ask for those files, and the transaction is completed more quickly as a result.

Some things you should know about HTTP/2 Push:

  • Due to the fact that we’re using HTTP/2, the only URLs that are eligible to be pushed must be HTTPS URLs.
  • The main site and the files to be pushed all need to be part of the same site URL. For example, if your site is served by LiteSpeed Web Server from https://example.com but the CSS is served by a content delivery network from https://cdn.com/example, HTTP/2 Push will not work. The main site and all of the files to be pushed will need to be served from the same place.

As far as that second point goes, we’ve recently introduced CDN support, and will explore it further in a future issue of WordPress Wednesday!

How do I enable these features?

The Optimize options are all turned off by default because they may have unexpected results. None of these methods should impact your code’s ability to function, but like we said, it can be unpredictable, so please test thoroughly before enabling minification or combination on your production site!

From your WordPress Dashboard, navigate to LiteSpeed Cache > Settings and select the Optimize tab (or press 5).

CSS Minify

Extra white space characters, new line characters, and comments will be stripped from all CSS, if this option is turned on.

CSS Combine

All individual CSS files will be combined into a single CSS file.

CSS HTTP/2 Push

CSS will be sent to the browser before it is requested.

CSS Excludes

Use this space to list any CSS files (one per line) that should be excluded from the above three functions. You may enter full URLs or a partial string. There’s no need to use wildcards in partial strings.

JS Minify

Extra white space characters, new line characters, and comments will be stripped from all JS, if this option is turned on.

JS Combine

All individual JS files will be combined into a single JS file.

JS HTTP/2 Push

JS will be sent to the browser before it is requested.

JS Excludes

Use this space to list any JS files (one per line) that should be excluded from the above three functions. You may enter full URLs or a partial string. There’s no need to use wildcards in partial strings.

CSS/JS Cache TTL

The minified and combined files are cached. This setting specifies for how long. The minimum is 3600 seconds, but we recommend 604800. When a Purge All command is issued, the minified and combined CSS/JS are included in that purge.

HTML Minify

Extra white space characters, new line characters, and comments will be stripped from all HTML, if this option is turned on.

What if I’m using other plugins that include some of these services?

There are many optimization plugins in the WordPress Plugin Directory, and they all have their own unique sets of features. Many of the individual options of any one plugin, however, may overlap with the individual options of the others.

As long as the plugins you are using allow you to enable and disable features at will, then they may be used at the same time. Just be sure not to use the same features in more than one plugin at a time.

Besides the fact that it just doesn’t make sense to have two plugins running the exact same optimization steps, it also has the potential to introduce buggy behavior.

Pick a plugin to do a particular optimization technique and then disable that technique in all others.

We’ve talked a little bit about this before, if you want to read up. At the time of that writing, we didn’t have some of the great extra features that we do now. Today we are better equipped to be your one-stop optimization shop than we’ve ever been! And we’re not finished yet, so stay tuned!

Have some of your own ideas for future WordPress Wednesday topics? Leave us a comment!

Don’t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:

Comments

27 responses to “WpW: Minification, Combination, and HTTP/2 Push”

  1. Jon Avatar

    Just wanted to say that I love this series and what you guys are doing with the wordpress plugin, keep up the good work!

    1. Lisa Clarke Avatar
      Lisa Clarke

      That makes us happy to hear 🙂 Thanks so much for the feedback!

      1. Jon Avatar

        Hey guys, I just updated to version 1.3.1 and am no longer able to successfully use any of the css and js optimizations. Before the update I had CSS Minify, combine, push and JS minify, combine, push all turned on and working well. After the update turning on any of the CSS options breaks the formatting of the site and turning on any of the JS options breaks the mobile menu. I am still able to leave the HTML minify and remove query strings options on. (I have chosen not to use the load CSS asynchronously as I use a Google font and also not to use the load JS deferred option as it made things a bit wonky when the page was first loaded.) I also have the new remove wordpress emoji option on as well as browser caching.

        1. Lisa Clarke Avatar
          Lisa Clarke

          Hi, Jon
          Thank you for providing the details of what works and what does not. We’ve gotten a few other similar reports overnight. I’m not in the office today, but our WP developer is on the case.
          I apologize for any inconvenience!
          Lisa

        2. Lisa Clarke Avatar
          Lisa Clarke

          Oh! I just saw this: https://wordpress.org/support/topic/notice-about-v1-3-1-optimizer/
          Hopefully this will solve the problem for you!

          1. Jon Avatar

            Thanks, I will check it out!

  2. amit Avatar

    I have implemented litespeed on my site. Its working great but the only issue is that when I combine css or JS my website takes more time to load as compared to the one with no combine. Can you please hep me with this.

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, amit. We’ll need some additional information in order to help you with this, and blog comments are not the most convenient place for troubleshooting. Would you please create a ticket (or send email to bug at litespeedtech.com? Thanks!

  3. Joan Avatar
    Joan

    Ok, so my question is about the HTTP/2 Push option versus using a CDN.

    Are you saying that if I choose to use HTTP/2 Push, then even if I have a single image being served on a page from, say, Amazon Cloudfront, then HTTP/2 push will not work for that page?

    If that is true, then it begs the question: Which is better to use – HTTP/2 Push without a CDN, or CDN without HTTP2/ Push?

    My guess would be since LSCWP is pretty darn efficient, and since the benefits of using a CDN are not that substantial in most cases really, the HTTP/2 Push would be more beneficial overall. Maybe for graphics heavy sites, no.

    Thanks!

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Joan. Good question. As long as index.html and the related CSS/JS files are all being served from the same location (i.e. your site), it doesn’t matter whether there are images from Amazon Cloudfront (or wherever else).

      Our HTTP/2 Push settings only apply to CSS and/or JavaScript, so it’s possible to use HTTP/2 for those two types of files, and then use a CDN for images and any other static files you may have.

      HTTP/2 Push has the benefit of reducing the number of requests that must be sent to the server. A CDN doesn’t do that, but it does theoretically reduce the distance that the requests have to travel. Which of those things is more beneficial depends a lot on the content your site needs to serve. Like you said, image-heavy sites would probably see more benefit from a CDN, while others might be better off with HTTP/2 push. But, there’s nothing stopping you from using them together, as long as you exclude the CSS and JS files from your CDN.

      I hope this clears it up a bit!

  4. WpGurme Avatar

    Hello

    At last update we have Image Optimization, its so good but when i try optimization request turn “Failed to push to LiteSpeed server: Previous optimization requests remaining in queue: 3086”. Where we have problem, screenshot: http://prntscr.com/h4585v

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi. Are you saying that this is the first time you have requested optimization and you are seeing this error? Would you mind creating a ticket (or send email to bug at litespeedtech.com)? It might be easier to look into this that way. Sorry you are having a problem!

  5. Andaç Avatar

    I think excluding url feature under the tuning tab doesn’t work properly.

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Andaç. Why do you say that? Can you give us an example that we can take a look at? Thanks!

      1. Andaç Avatar

        I have a radio player in my two page. When i open combine js and add js and html links of the player in “exclude url” from Tuning tab, players showing in the bottom of the page.

  6. SchaOn Avatar

    How do you manually put in to have a file pushed?

    For example, I put this in the .htaccess file:
    Header add Link “; rel=preload; as=font; type=font/woff2”

    that’s not pushing 🙁 Though the browser is finding out right away that it needs to download it, so it’s brought it faster, but not as fast as if it was pushed.

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Schaon

      We don’t currently support font push. However, you might find what you’re looking for on stack overflow.

      Good luck!

  7. […] LiteSpeed Cache plugin also enables you to do Minification and Combination, which is also compatible with OpenLiteSpeed, you can read more about this feature here. […]

  8. Ashwin Singh Avatar
    Ashwin Singh

    So, I am running a site which generates money from Google Adsense. Right now litespeed cache plugin pushes the adsbygoogle.js to the bottom of the page which may add a slight delay in ad load.

    Is there any way to specify the order of JS. I would love to push the adsbygoogle,js to the top of the page.

    1. Lisa Clarke Avatar
      Lisa Clarke

      Hi, Ashwin. If you exclude adsbygoogle.js from optimization, then it will be left alone to run wherever it is. Hope this helps!

  9. çıplak yazar Avatar

    Hey

    When i am turn on js combine my site is giving error.

    Something wrong but i didnt understood.

    Also i checked my all js files and try to js Excludes them, but still same problem.

    How can i fix that?

    1. Lisa Clarke Avatar
      Lisa Clarke

      I’m sorry to hear you’re having a problem. If you’ve excluded all JS, purged the cache, and are still having an error, maybe JS Combine is not the problem. Have you enabled any other optimizations?

      Please try following the steps in this guide. It should help you pinpoint the exact file that is at fault.

  10. ali ft Avatar

    I activated the options
    But it doesn’t work

    1. Lisa Clarke Avatar
      Lisa Clarke

      I’m sorry to hear that. Did you purge the cache after activating the options? If so, and it still does not appear to be working, please open a ticket. Thanks!

  11. Manuel Dinis Júnior Avatar

    Thank You So Much, It was very helpful

  12. Takinmall Avatar

    thank you for your post
    I got a bad pageSpeed in CSS modification.
    I need to know I should OFF these features?
    https://mag.takinmall.com

    1. Lisa Clarke Avatar
      Lisa Clarke

      Enabling CSS optimization shouldn’t cause your pagespeed score to get worse. You could try disabling the CSS settings and see if that makes a difference. If turning the features off gives you a better pagespeed score, then something odd may be going on with one of your CSS files. You can try to figure out which one using this method.

Leave a Reply

Your email address will not be published. Required fields are marked *