Sinatra - Render index.html on every request

418 views Asked by At

I'm a little stumped right now, it's one of those moments where my app was working fine yesterday, but all of a sudden it's not, and I have no idea why (I'm sure you can relate).

module SK
  module Routes
    class Base < Sinatra::Base
      include Models

      get '/*' do
        File.read 'public/index.html'
      end

      helpers Helpers::API
    end
  end
end

I'm building an Angular app, so I need to serve the index.html on every request. This all works fine when I'm using the shotgun gem, but as soon as I put it in production using foreman, it serves everything, including the assets, as index.html.

Here's my Procfile:

web: bundle exec rackup config.ru -p $PORT

Here's my config.ru:

require './app'

run SK::App

So I don't understand why it works in development, but not in production.

Any ideas?

1

There are 1 answers

1
matt On BEST ANSWER

Shotgun has its own static server that it will use (to avoid forking when not required). Sinatra will also serve static files if configured to do so, but using the modular style app disables this by default.

The fix is to enable the static server:

module SK
  module Routes
    class Base < Sinatra::Base

      # add this line:
      enable :static

      include Models

      get '/*' do
        File.read 'public/index.html'
      end

      helpers Helpers::API
    end
  end
end

Now Sinatra will serve static files from the public dir before trying to match the route.