Static Sites on Heroku in Two Lines 29

Posted by Marshall Huss Mon, 14 Dec 2009 02:39:00 GMT

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

  1. Avatar
    John 1 day later:

    Is it possible to force clear a particular page or image from the cache?

  2. Avatar
    Marshall 1 day later:

    Yes, every time you push to Heroku and deploy the entire cache is expired.

  3. Avatar
    John 1 day later:

    Yeah I heard about the push method

    Just seems scary to need to wipe the entire cache to clear one asset :)

  4. Avatar
    Bryan 5 days later:

    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 :-)

  5. Avatar
    Scott 7 days later:

    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?

  6. Avatar
    Scott 7 days later:

    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.

  7. Avatar
    Marshall 8 days later:

    Thanks @Scott, I will add those notes to the original entry.

  8. Avatar
    Mike Bailey 15 days later:

    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”

  9. Avatar
    wadsdsa 10 months later:

    dd those notes to t

  10. Avatar
    wadsdsa 10 months later:

    roblems with the following fields:

  11. Avatar
     gps tracking systems 10 months later:

    This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article.  gps tracking systems

  12. Avatar
    Refrigerator service 10 months later:

    Yes! This is time to show qt’s misrable in the real appliance, not about license

  13. Avatar
    Samual 10 months later:

    Of course, we need a static website when works,but sometimes it cant do as we wish using the heroku.

  14. Avatar
    Tpvtumhi about 1 year later:

    Some of its members also had punk rock background. , grady white jell coat.com, [url=”http://hg2.stonehitaccess.com/grady-white-jell-coat.com.html”]grady white jell coat.com[/url], http://hg2.stonehitaccess.com/grady-white-jell-coat.com.html grady white jell coat.com, slqi,

  15. Avatar
    programy about 1 year later:

    Thanks for this great post. Did you do it all on your own? This must’ve taken a lot of time

  16. Avatar
    darmowe pozycjonowanie about 1 year later:

    This is the greatest topic I have read today

  17. Avatar
    darmowe krzyzowki about 1 year later:

    Thanks for this great post. Did you do it all on your own? This must’ve taken a lot of time

  18. Avatar
    frenky about 1 year later:
  19. Avatar
    David about 1 year later:

    Thank you for this information. I’ve been looking for something like this for quite a while. Keep up the good work, cheers! klimat thailand lången tips filippinerna

  20. Avatar
    Erik about 1 year later:

    Cheers man, I’ve been looking for a guide like this for quite some time now. reservdelar mg reservdelar mini reservdelar land rover

  21. Avatar
    DR JONES about 1 year later:

    Thanks for a good post. Keep em coming bro! synas på google places restaurang karlstad

  22. Avatar
    DR JONES about 1 year later:

    Thanks for a good post. Keep em coming. Hope this blog will never come to an end. synas på google places restaurang karlstad

  23. Avatar
    Jones about 1 year later:

    This is some great blog. Too bad you stoped blogging. synas på google places

  24. Avatar
    Joseph Raymond about 1 year later:

    Now thats an easy way to get a static page. And it is cached. What more i am going to ask

  25. Avatar
    Kkmfugig about 1 year later:

    It’s a bad line youngchild bra models >:]]]

  26. Avatar
    Sfkrpdea about 1 year later:

    I’ve been made redundant Killah Bbw Toplist >:-P

  27. Avatar
    Lzmlnkzz about 1 year later:

    I enjoy travelling Lily Model Dvd 023651

  28. Avatar
    tv guide listings about 1 year later:

    Great post again! Thank you for sharing this info with us!

  29. Avatar
    bilverkstad växjö about 1 year later:

    Good share again! Static sites is just about this stuff. Thanks. Bilverkstad i växjö.