Jonah
Jonah

Reputation: 16212

Rack::Reloader not picking up changes

Consider this config.ru file:

require 'sinatra'

use Rack::Reloader, 0

get '/' do
  'hi'
end

run Sinatra::Application

I start it from the command line with thin start. The application runs and shows hi when I hit localhost:3000. If I now change hi to hello, save the file, and reload the page, the change does not appear: the page still says hi.

Why does Rack::Reloader not work in this case? Can I change anything to make it work?

Upvotes: 3

Views: 986

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59611

See here for a detailed explanation of what's happening. Essentialy everytime your file is changed, Rack::Reloader re-requires it.

Unfortunately with Sinatra, if you redefine a route a second time (which is what happens when you re-require), sinatra ignores the new definition since get '/' do end is already defined!

What you will need to do is reset any defined routes you have as well:

# inside app.rb

require 'sinatra'
require 'rack'

configure :development do
    Sinatra::Application.reset!
    use Rack::Reloader
end

get '/' do
  'hi'
end

Note that sometimes it takes a few seconds (5s on my machine) for the changes to be reloaded and I recommend you take a look at the alternatives here

Upvotes: 6

Related Questions