Static Sites on Heroku in Two Lines 8
Sometimes you just have a static website with one or a couple pages. You might even have a shared hosting plan or even a VPS but who wants to go through the hassle of that when you could use Heroku?
Here is a simple way to host your static site and cache it on Heroku using a Rack app.
Here is how your folder should be organized:
In the config.ru file add the following:
This assumes that your template uses relative references to the images and stylesheets. Go ahead and deploy the app, if you are not sure how to deploy to Heroku check out their quickstart guide.
Now since Heroku uses a HTTP Accelerator we should take advantage of it. If you noticed in the config.ru file we set the Cache-Control header to 86400 seconds (24 hours). This tells the accelerator to go ahead and cache the page. The accelerator already caches any Rack::File for 12 hours as well meaning all your stylesheets and images will also be cached.
To confirm it's working, let's take a look at the HTTP headers returned from your site. The Age header indicated that it's being served from the HTTP cache.
And there you go, a static site being served in Heroku's cloud completely cached and for free.
Trackbacks
Use the following link to trackback from your own site:
http://mwhuss.com/trackbacks?article_id=9



Is it possible to force clear a particular page or image from the cache?
Yes, every time you push to Heroku and deploy the entire cache is expired.
Yeah I heard about the push method
Just seems scary to need to wipe the entire cache to clear one asset :)
Nice post. If you want to use jekyll pages on Heroku or any vps server , try Rack-Jekyll.
It can recompile pages if _site directory is emptied and it supports custom 404 page. You can add any rack middleware with it like Rack::Cache and others :-)
That works great for a one page site. How do I make it work for, say, a three page site with index.html, about.html, and contact.html?
Okay, here’s a solution. I don’t know anything about Rack, but I added the two pages to the :urls array argument like this:
use Rack::Static, :urls => [“/stylesheets”, “/images”, “/about.html”, “/contact.html”], :root => “public”
And now it works. Had to refresh each page, probably because of the caching.
Thanks @Scott, I will add those notes to the original entry.
Nice find Marshall.
If you want to serve everything, just drop a regex into :urls.
This will find and serve everything :-)
use Rack::Static, :urls => [/./], :root => “public”