Serving up static sites on heroku - redux

1 minute read

First off, I should probably tell you that I totally ♥ Heroku. I run a bunch of little apps off it and at work believe it’s production ready for our main platform. A while back someone posted a nice little snippet of how to get a static site working on it in two lines of code on heroku, however, with some change in heroku, gem packaging or the like (or fact I added in multiple custom domains for the site), these static served pages suddenly stopped working. Counterintuitive when looked at, and even @radar and I together looking at it couldn’t figure it out. Anyhow, I puzzled the following out. Probably a few of the gems can be removed but this definitely worked for me.

1
2
3
# .gems*

rack-rewrite --version '>= 1.0.0'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# config.ru*

gem 'rack-rewrite', '~> 1.0.0'
require 'rubygems'
require 'rack'
require 'rack/rewrite'
require 'thin'

use Rack::Static, :urls => ["/css", "/images", "/index.html", "/contact.html", "/home.html", "/password.html"], :root => "public"

use Rack::Rewrite do
  rewrite '/', '/index.html'
end

run Rack::Directory.new('public')

This assumes you have a directory structure similar to the following :

1
2
3
4
5
6
.
|-- config.ru
`-- public
    |-- index.html
    |-- css
    `-- images
dev ruby devops