Deploying Jekyll sites to Heroku with Rack-Jekyll


Note: Check out my article on the Twin Engine Labs engineering blog for some Jekyll highlights and a mini-guide on creating your first blog post.

This will just a quick guide to show how to get your Jekyll site going on Heroku. I’m going to be working off the assumption that you’ve already got a Heroku account setup, so I won’t be going through any of that here.

First you need to install Bundler and add a Gemfile to your project if you don’t already have one. The gem we need is called Rack-Jekyll. It basically just provides a little Rack middleware to serve your statically-generated site to Heroku’s thins. Unfortunately the latest version in Rubygems depends on some very old versions of gems so you’ll need to install from git.

Add the following line to your Gemfile:

gem 'rack-jekyll', :git => 'https://github.com/adaoraul/rack-jekyll.git', :require => 'rack/jekyll'

Next create a file named config.ru in your project, if it doesn’t already exist, add the following to it:

require 'bundler'
Bundler.require(:default, :production)

run Rack::Jekyll.new

The guide on GitHub is different than the above advice, but I kept getting errors when it tried to require ‘rack/jekyll’ until I modified my config.ru to look like this.

Now you should be clear to deploy to Heroku as usual (git push heroku master).

An interesting sidenote

Up until recently I was deploying my site with rack-jekyll version 0.3.5 (current version in RubyGems is 0.3.7). For a long time I was experiencing an issue where any images I uploaded (after switching to this deploy method) had the bottom 10% or so cut off when served from Heroku. At first it was just a single image and I thought it just a fluke (and I hadn’t written a new blog post in a long time), so I didn’t mess with it. But then with my latest blog post I ran into the same issue again.

I finally contacted Heroku support, and after a bit of back-and-forth they determined that Rack-Jekyll was responding with an incorrect Content-Length header. I tried upgrading to 0.3.7 immediately, but the RubyGems version called a whole new slew of headaches, so I gave up and dug into the code. Turned out that the old version was reading in the file, turning the body into a string, calling .length off that string, and passing that as the content length. This is the incorrect way to handle this, and there are a couple of better ways to do it. Since we’re reading the file in anyways, an easy way is just to use file.size. Another option, if you’re using a string, is to call "string".bytesize.

Anyways, I eventually discovered that the version of Rack-Jekyll on GitHub had been updated to current versions of the important gems, and already had the fix in place, so I just switched over to using that instead.

Cool story bro.

subscribe! reddit! hacker news!

blog comments powered by Disqus
Fork me on GitHub