Posted on Wed 29 February 2012

gzip your Octopress

When I read Hacker News today, I found a very cool post: Make your website fast. (And why you should).cache Of course, I wanted to try this out myself - one of the major reasons I went to S3 for hosting was increased speed.

Now by default, octopress doesn't include anything to help you here - S3 requires that you gzip your static content files manually. Aditionally, you have to set the correct content header! Now, a bit of googling quickly led me to an excellent post from Frank Fusion.cache (After some trial and error with the aws/s3 gem from ruby, which I scrapped because it didn't work with the european S3 server)

He has a very nice bash script to compress and upload all the files, which I adapted a bit for Octopress (his version is for Jekyll). It's actually very simple:

#!/bin/bash

# compress the files if they aren't
find public/ -iname '*.html' -exec gzip_if_not_gzipped {} \;
find public/ -iname '*.js' -exec gzip_if_not_gzipped {} \;
find public/ -iname '*.css' -exec gzip_if_not_gzipped {} \;
# change their name back
find public/ -iname '*.gz' -exec rename 's/\.gz$//i' {} +

# sync gzipped files
s3cmd sync --progress -M --acl-public --add-header 'Content-Encoding:gzip' public/ s3://$1/ --exclude '*.*' --include '*.html' --include '*.js' --include '*.css'
# sync non gzipped files
s3cmd sync --progress -M --acl-public public/ s3://$1/ --exclude '*.sh' --exclude '*.html' --exclude '*.js' --exclude '*.css'

Call it like this: ok_failed system("./gziped_sync.sh #{s3_bucket}"). If you are wondering about the find command - gzip_if_not_gzipped is a little script I wrote to only compress a file if it's not already compressed - otherwise, we always have to completely regenerate the whole site.

#!/bin/bash
file $1 | grep "gzip compressed data" > /dev/null
if [[ $? != 0 ]] ; then
    # only gzip if it's not already gzipped
    gzip -n $1
fi

With this little trick, uploading to S3 is as fast as before, while we still have the advantages of gzipped content. Nice :)

Category: misc

Tags: web

© Julian Schrittwieser. Built using Pelican. Theme by Giulio Fidente on github. .