views:

323

answers:

4

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.

+1  A: 

I use Microsoft Ajax Minifier which comes with a C# library to minify js files. I use that on the server and serve up a maximum of two minified .js files per page (one "static" one that is the same across the whole site, and one "dynamic" one that is specific to just that page).

Yahoo's YUI compressor is also a simple Java .jar file that you could use as well.

The important thing, I think, is not to do it on a file-by-file basis. You really do need to combine the .js files to get the most benefit. For that reason, an "automatic" solution is not really going to work - because it will necessarily only work on a file-by-file basis.

Dean Harding
The main thing is that I'd rather not have to run a script manually (yeah I know lazy). I already only have one js and one css file per page.
Brendan Long
+3  A: 

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.

Mark Maunder
A: 

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).

Brendan Long
+2  A: 

If you use Nginx instead of lighttpd then you can take advantage of Nginx's embedded Perl support to leverage the Perl module JavaScript-Minifier to minify and cache JS server-side.

Here are the details on how to achieve this: wiki.nginx.org/NginxEmbeddedPerlMinifyJS

warwickp