This is a Rails gem that reloads browser as soon as you do some changes in your Rails app. We as web developers are sometimes bounded in Change -> Switch to Browser -> Refresh -> Change
cycle. This really hurts productivity. This gem will help you in breaking this cumbersome routine and your browser will be refreshed automatically whenever it detects any change in Rails code thus freeing you from manually updating browser and hence resulting in increased productivity.
Add this line to your application's Gemfile:
gem 'active_reloader'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_reloader
After gem installation just enter following line into your project's application.js
//= require active_reloader
By-default ActiveReloader
looks for changes in app
directory. You can easily add / modify directories according to your choice. To update directories list, first create an initializer
in your Rails app and use following code:
ActiveReloader::Configuration.update do |config|
config.paths << ["config", "db"]
end
You can also use following way to provide single directory:
config.paths << "config"
If you want to completely replace default directories and provide new ones you can use:
config.paths = "config"
# or
config.paths = ["config", "db"]
Always provide directories relative to your Rails app. For example if you want to look for changes in config/initializers
directory you can use following code:
config.paths << "config/initializers"
ActiveReloader
runs on separate server than that of your Rails app. As soon as you starts your Rails app a separate server will automatically be created for serving ActiveReloader
requests. ActiveReloader
's server run on Rails' port + 1
. This means if you have run your Rails app on port 3000
then ActiveReloader
will automatically run on port 3001
. Similarly if you choose port 5555
for your Rails app, ActiveReloader
will use port 5556
.
The reason of running ActiveReloader
on a separate server is quite obvious. It don't want to interfere with your application code thus your Rails server don't need to serve requests related to ActiveReloader
. The second reason is Rack::Lock
middleware. This middleware is inserted by-default by Rails and because of this no other request can get processed until current running request is finished. Since ActiveReloader
uses Ajax Long Polling to look for changes in your code, which means if ActiveReloader
's Ajax request is getting processed your Rails app's requests will not be entertained. So the obvious solution is to use a separate server for doing stuff related to ActiveReloader
.
You might be wondering why Ajax Long Polling when there are options available like WebSockets
and Server-Sent Events
. The answer to this question is, Simplicity. Doing Ajax requests seems quite natural way of doing things when it comes to getting updates from server. Other solutions require some configuration, but there is no need to do any for Ajax and it works on every Ruby based web-server. Since ActiveReloader
's server is running locally it can serve requests at a blazing fast speed.
To increase performance of ActiveReloader
's server, instead of doing Ajax requests after each 01 second I opted Ajax Long Polling, which upon requests arrival will keep looking for changes in a loop for 25 seconds and will break as soon as any change is detected.
active_reloader.js
upon it's execution creates an iframe
which points to ActiveReloader
's server. Upon any change found ActiveReloader
's JavaScript communicate with parent window
by using postMessage
which is considered to be the best option for communicating between parent and child windows.
ActiveReloader
is intended to be used only in development
environment. It is not developed to be the next hot code reloader / swapper. Necessary checks have been placed to make sure ActiveReloader
only works in development
environment when Rails server is running.
Special thanks to following awesome developers for testing ActiveReloader
gem.
- Fork it ( https://github.com/ilatif/active_reloader/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request