Tweaking for Page Speed with CGI and thttpd
Yesterday I checked the Page Speed for this site using the Firefox add-on. Even though pages seem to load fast the score was (somewhat disappointingly) in the 80s. So last night I set out to do something about it.
The first big issue was that Page Speed told me I had to "enable compression". The problem with that is the webserver -- thttpd -- doesn't support compression. As thttpd couldn't do it, I thought maybe Python could (the blog is written in Python). A half-hour of Googling lead me to cgi_buffer, a set of libraries for Perl, Python and PHP that (among other things) provide gzip content compression. After just a little experimentation and testing I was getting gzipped output from the blog script.
The score increased, but Page Speed still nagged me about the linked stylesheet being uncompressed. I couldn't think of a way to fix that so I worked around it. I rewrote the stylesheet to make it as small & efficient as I could, the ran it through a CSS compressor to strip out unnecessary whitespace & punctuation. That brought it down to about 3.6 KB. Then I removed the linked stylesheet from the blog script and added a few lines of code to read my CSS file into an embedded stylesheet. So now the CSS is compressed along with the rest of the HTML output.
Leverage Browser Caching
With compression out of the way I moved on to the next issue: browser caching. Page Speed didn't like the fact that my images were being served without a "far future" expiration date. thttpd has a configuration setting for expiration time ("max_age"), but it's a global setting. I experimented with it, and as expected it proved a nuisance for standard HTML pages (other stuff outside of this blog). I was about to give up when I remembered something I'd read in the thttpd documentation....
You can run two or more instances of thttpd, each with it's own configuration. I made a copy of the thttpd config file and in that copy made a few changes: I set the listen port to 8000 and the "max_age" to 2592000 seconds (30 days). Then I made some adjustments in the blog script and the CSS, so that images would be served from linux.dashed.ca:8000. It worked! Images now have a far-future expiration date and the Page Speed score took a big jump.
There were a few more items needing work, such as image optimization, but those were pretty standard stuff. Once done I ran Page Speed a final time, and here's the result:
I think I'll live with those 222 bytes of excess transfer :)