zhuyxn
zhuyxn

Reputation: 7081

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)   ] }

Upvotes: 2

Views: 1798

Answers (2)

Dmitry Pashkevich
Dmitry Pashkevich

Reputation: 13516

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.

Upvotes: 0

robertjlooby
robertjlooby

Reputation: 7220

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)

Upvotes: 3

Related Questions