You can use applications like the YUI Compressor to maintain a stripped down version of your CSS in a separate file which is served to browsers, but that requires you to run the compressor after each modification you make, which is tedious. If you generate your CSS dynamically on the fly from a template file for example, you have further difficulties.
I wrote a mod_perl based output filter which compresses CSS on the fly using CSS::Minifier::XS. I have it running on this very website at the moment. You can see how it compresses a CSS file here:
If you add â€œuncompressedâ€ to the arguments then it will show you the response as it appears on disk, without it being compressed on the fly:
At time of writing, the difference in size between the two responses is 28029 bytes vs 37645 bytes. You might say that 9.4KB isnâ€™t a lot in these days of broadband access, but every little helps. On a dialup connection that single CSS file compression would knock two seconds off the page load time.
You can take a look at the module I wrote, and the Apache configuration by looking here CSSOutputFilter.pm
The examples above don't actually work at the moment because I have removed mod_perl from my server to free up memory. You can still download the source code and use it yourself though.
If you want to read more stuff like this, follow my blog or check out the rest of my articles: All, Perl related, Web related. If you found this article helpful, interesting or entertaining, and wish to donate:If you want to leave a tip: