Using Plone? Compress all of your JavaScript resources with Varnish

published Jul 17, 2012, last modified Jun 26, 2013

Plone's JavaScript resource registry can gzip almost all of its JavaScript resources. But one does not get compressed.

And that one is pretty big.  It's the jQuery file, and it's huge by Web standards.  Compressing it would save 80 KB per request.  That may mean up to a quarter of a second of waiting, or even more in the case of mobile visitors to your site.

The problem with Plone (as of 4.1.5) is that there's a long-standing bug (in fact, it's bug #2 in the plone.app.caching tracker) where JavaScript resources larger than 64 KB do not get compressed.  Guess which file makes the grade?

Well, we can't wait until the Plone developers fix it at their characteristic glacial pace, so we'll hack around it with Varnish.  It's pretty easy to do with Varnish 3's automatic gzip compression support.

The following VCL does the trick:

sub vcl_fetch {

        if (req.http.host == "yourhostname.com" && beresp.http.Content-Type ~ "(text/|application/x-javascript)") {
                set beresp.do_gzip = true;
        }

}

All we're doing here is telling Varnish that, after having fetched, and prior to inserting it into the cache, would it please gzip any object with a text MIME type or JavaScript script.  This gets the object compressed (and maybe inserted into the cache, depending on your caching policy).

Presto!  JavaScript space savings complete!