zengun

weblog

20040810

gzipping CSS

When there’s more than one way to do it, for the love of Pete, don’t use the worst way to compress your CSS files (using PHP).

Using PHP’s ob_gzhandler will indeed compress your stylesheet.
But let’s look at the costs first:

  • CPU cost: PHP will have to parse a file where it only needs to execute one or two functions.
  • Browser/proxy cache cost: PHP won’t send the right HTTP headers by default, so the file will never get cached right. Traffic costs will actually rise unless your visitors only visit one or two pages on your website.
  • Valuable time cost: do you really want to add these lines of PHP to each and every CSS file you serve?

Horrific, isn’t it?
Instead, here’s a solution that only takes one line in your .htaccess, and mod_gzip:

mod_gzip_item_include	file	\.css$

There, instant compression on all your .css files.
This may sound like the obvious solution, but still too many are using PHP for this task because they didn’t know better, so I felt I had to blog it…


2 responses

  1. When I was gzipping my CSS and JS files, certain browsers ewren’t happy about it.

    #1 Alex2004/08/10 at 16:30

  2. Just wanted to ask sth. I have a problem with my css, being big and was wondering can i do the same tricks with gzipping for php-nuke as well?
    and how? cheers

    #2 StealthGR2005/06/20 at 17:18

Your words


Required fields:
Are marked by this sign: *
XHTML:
You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
On comment spam/irrelevance:
Your comment may be moderated or considered as spam by the leprechauns running this server. If you get a 412 error or a mean message, it may be that they thought it was spam: try wording the comment differently or remind me to put up a contact form so you can warn me about the problem.
Of course, once the leprechauns are done doing their magic, I reserve the right to delete any comment for any reason.