Categories
Code

Speeding Up Your Heroku Jekyll Site

Something I missed about hosting my site on an Apache server, was how easy it was to turn on compression and caching. I finally spent an afternoon a few months ago to figure out how to do that on Heroku. It’s actually pretty simple. In fact, it’s easier than Apache, it’s just that the unknown can be scary sometimes.

config.ru Is Where the Magic Happens

Open up your config.ru, that’s where we’ll be putting the code. First thing we’ll do is to enable Rack::Deflater.

# config.ru

require 'rack'
use Rack::Deflater

This part of the file enables compression on our assets. We’ll make that work in the next part.

#config.ru

use Rack::TryStatic,
  :gzip => true,
  header_rules: [
    [:all, {'Cache-Control' => 'public, max-age=86400', 'Vary' => 'Accept-Encoding'}],
    [['css', 'js'], {'Cache-Control' => 'public, max-age=604800'}]
  ]

The gzip part lets us use the compression we enabled earlier, and header_rules caches the hell out of everything, so that people don’t have to re-download resources.

That’s it! With only a few lines of code, you can drastically speed up your site on Heroku.

By Timmmmy

A human hoping to be empathetic and kind with all. I'm a geek, coffee drinker, stoner, maker of things, and forever an optimist. Brightly-colored clothes weren't enough, so I dye my hair too (he/him)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s