Server-side auto-minify?

Brendan Long picture Brendan Long · May 26, 2010 · Viewed 11k times · Source

Is there any way to automatically minify static content and then serve it from a cache automatically? Similar to have mod_compress/mod_deflate work? Preferably something I could use in combination with compression (since compression has a more noticeable benefit).

My preference is something that works with lighttpd but I haven't been able to find anything, so any web server that can do it would be interesting.

Answer

Mark Maunder picture Mark Maunder · May 26, 2010

You can try nginx's third party Strip module:

http://wiki.nginx.org/NginxHttpStripModule

Any module you use is just going to remove whitespace. You'll get a better result by using a minifier that understands whatever you're minifying. e.g. Google's Closure javascript compiler.

It's smart enough to know what a variable is and make it's name shorter. A whitespace remover can't do that.

I'd recommend minifying offline unless your site is very low traffic. But if you want to minify in your live environment I recommend using nginx's proxy cache. (Sorry but I don't have enough reputation to post more than one link)

Or you can look into memcached for an in-memory cache or Redis for the same thing but with disk backup.