Me wearing ridiculous goggles

Content Gzip Plugin for Jekyll

To serve up static pages generated by Jekyll, I use my favorite static-file webserver: publicfile. To save my bandwidth and your time, I wanted to support gzip compression. The gzip compression patch for publicfile does it in the simplest possible way: it checks for a .gz version of every file and sends you that if it can (and if your browser supports it). Producing all these files by hand got tedious, so I wrote this generator plugin for Jekyll.

The Plugin

The plugin’s goal is to create compressed versions of the files that make up your site, alongside the uncompressed version — but only when it makes sense to do so. For example, if your site currently contains these files:

index.html
logo.gif
fonts/xyz.ttf
posts/date/foo.txt

…the plugin will add compressed versions of the compressible files:

index.html
index.html.gz
logo.gif
fonts/xyz.ttf
fonts/xyz.ttf.gz
posts/date/foo.txt
posts/date/foo.txt.gz

This approach is nice for two reasons:

  1. It works with the patched version of publicfile, so users transparently get gzip-compressed data if their browser can handle it (and most browsers can).
  2. It maintains the Jekyll philosophy by taking the time to gzip everything once, at deployment, rather than at every request.
  3. The compressed files are also available, so you can download the compressed version to your user’s machine by linking directly to your-file.foo.gz.

The plugin decides what to compress based on file extension (using a hardcoded list of compressible types) and size — very small files are never compressed.

Unfortunately, when Jekyll finds multiple generators, it runs them in random order. Thus, this plugin may or may not find and compress the output from other plugins.

Implementation Notes

This version works with a unmodified Jekyll 0.10.0. It has to perform some monkey-patching to make Page, Post, and StaticFile implement a common interface so it can treat them polymorphically. (This seems like an obvious thing to improve in later versions of Jekyll!)

As a result, there is a small possibility that the plugin will break on later versions. The monkey-patching is clearly marked in the source.

Files

More Cliffle

By Topic