Published in CSS on Tuesday, February 17th, 2004
Note: The "official line" on this topic is being archived and updated here.
Note: This entry was updated on the 23rd of February. A summary of the process can be found here.
Late last year I had the pleasure of redesigning a site that I'd coded a mere 6 months earlier. Talk about a humbling experience, it seems that I had a bad case of classitis during the first design. I spent a day removing useless extra markup, and in the end the site had much lighter and cleaner pages, with one exception: the CSS had 'bloated' from just under 7kb to just over 12kb.
Although not anything too extreme, others began to throw wood on the fire, and I began looking for a way to make things smaller. I googled for solutions, and came up with...
I looked into using mod_gzip or mod_deflate with Apache. With some help from Stephen Pierzchala at WebCompression.Org, I managed to get it working at home. Unfortunately, my hosting provider wasn't in to setting it up for me. So that was it then, no compression for me, until...
Basically it talks about parsing your CSS file as a PHP document. While in the thread they were talking about customizing this and that, it hit me that this could be a workaround for my situation.
So I did some testing, and sure enough it works. The CSS file for a current project, with comments, is up to 15kb. Here are the post-compression results:
This part is quite simple, I think... that is, I haven't run into any problems yet!
<?php ob_start ("ob_gzhandler"); header("Content-type: text/css"); header("Cache-Control: must-revalidate"); $offset = 60 * 60 ; $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"; header($ExpStr); ?>
This code does 4 things:
Though not yet extensively tested, I think that this is quite reliable. I would love to hear feedback from people. However as I don't yet have comments for this weblog, it'll have to be by e-mail. Send comments and issues to email@example.com.
I started freelancing by diving in head first and getting on with it. Many years and a lot of experience later I was still able to take away some gems from this book, and there are plenty I wish I had thought of beforehand. If you are new to freelancing and have a lot of questions (or maybe don't know what questions to ask!) do yourself a favor and at least check out the sample chapters.
Like the other books listed here, this provides a great reference for the PHP developer looking to have the right answers from the right people at their fingertips. I tend to pull this off the shelf when I need to delve into new territory and usually find a workable solution to keep development moving. This only needs to happen once and you recoup the price of the book in time saved from having to develop the solution or find the right pattern for getting the job done..