In this article I will try to present two compression methods that together will make your scripts lose up to 80% of their weight and I will also present an alternative way to serve gzip compressed files with Turbogears : a precompressing gzip controller for Turbogears.
This sure is nice for a start, but we can go even one step further with gzip compression. Gzip compression is a w3 standard ( defined in RFC 2616 ) and can be used to send the output of a server compressed to a client. Using this our files shrink even more:
Uncompressed: 10.1 KB
Js and gzip compressed : 2.8 KB
Uncompressed: 329.0 KB
Js and gzip compressed : 57.8 KB
Most modern web servers allow you to enable general gzip compression. IIS does it, Apache has mod_gzip for it, Turbogears can do gzip compression via cherrypy’s gzip filter.
Personally this felt like an overkill to me. I don’t really want to use my precious server CPU cycles to compress output where it’s no larger win to do so. So I wrote myself a precompressing gzip controller for Turbogears. It creates gzipped copies of a configurable set of files and automatically updates them. If the client browser supports gzip encoding, the gzipped file copy is sent, otherwise the normal file is send.
The controller is meant to be used as a subcontroller list this
class Root( controllers.RootController ):
gz = GzipController()
This would map the gzip controller to /gz/ .
- gz.py — precompressing gzip controller
- serveFile.patch — Update: the patch has been integrated into the turbogears distribution