Ruby Rack Heroku: Serving Static Files

I have followed the Heroku guide on deploying static files using Ruby Rack (https://devcenter.heroku.com/articles/static-sites-ruby), but I was unable to access any HTML file in \public apart from index.html. Namely, localhost:9292/test.html still maps to index.html. (All my style and js files serve correctly).

Below is my config.ru file. I know what's wrong, but not sure about a valid solution?

use Rack::Static,    :urls => ["/images", "/js", "/css"],   :root => "public"

run lambda { |env|   [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)   ] }

Answers


For your config.ru file, try:

use Rack::Static,
    :urls => ["/images", "/js", "/css"],
    :root => "public"

run Rack::File.new("public")

You could also replace the last line with run Rack::Directory.new("public") which would serve up all your files, but would give a file browser like interface if someone went to the url of a directory (like the root directory)


I have no Ruby experience and I found this boilerplate project helpful when trying to deploy a static website to Heroku:

heroku-static-site

Particularly, make sure you also have Gemfile and Gemfile.lock files besides the config.ru.

Author's project structure hosts everything in the root directory but you can easily move everything to public/ and correct the config.ru as @looby suggested.


Need Your Help

$.when.apply on Array of Promises

javascript jquery ajax promise deferred

I have searched many very similar questions. I found one that had almost exactly what I was looking for except it did not have any error handling, and upon trying to implement error handling I am r...