Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-side auto-minify?

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.

like image 208
Brendan Long Avatar asked May 26 '10 00:05

Brendan Long


2 Answers

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.

like image 162
Mark Maunder Avatar answered Sep 17 '22 18:09

Mark Maunder


I decided to do this through PHP (mostly because I didn't feel like writing a lighttpd module).

My script takes in a query string specifying the type of the files requested (js or css), and then the names of those files. For example, on my site the CSS is added like this:

<link rel="stylesheet" href="concat.php?type=css&style&blue" ... />

This minifies and concatenates style.css and blue.css

It uses JSMin-PHP and cssmin.

It also caches the files using XCache if it's available (since minifying is expensive). I actually plan to change the script so it doesn't minify if Xcache isn't available, but I have Xcache and I got bored.

Anyway, if anyone else wants it, it's here. If you use mine you'll need to change the isAllowed() function to list your files (it may be safe to make it just return true, but it was easy to just list the ones I want to allow).

like image 34
Brendan Long Avatar answered Sep 21 '22 18:09

Brendan Long