2013-09-13 14:32:57 +00:00
# Rack::LiveReload
2012-06-05 09:19:48 +00:00
< a href = "http://travis-ci.org/johnbintz/rack-livereload" > < img src = "https://secure.travis-ci.org/johnbintz/rack-livereload.png" / > < / a >
2013-04-30 14:07:23 +00:00
[![Code Climate ](https://codeclimate.com/github/johnbintz/rack-livereload.png )](https://codeclimate.com/github/johnbintz/rack-livereload)
2012-06-05 09:19:48 +00:00
2011-11-04 15:51:22 +00:00
Hey, you've got [LiveReload ](http://www.livereload.com/ ) in my [Rack ](http://rack.rubyforge.org/ )!
No need for browser extensions anymore! Just plug it in your middleware stack and go!
2011-11-17 20:50:08 +00:00
Even supports browsers without WebSockets!
2011-11-04 15:51:22 +00:00
Use this with [guard-livereload ](http://github.com/guard/guard-livereload ) for maximum fun!
2013-09-13 14:32:57 +00:00
## Installation
2011-11-07 14:38:36 +00:00
2013-09-13 14:32:57 +00:00
### Rails
2011-11-07 14:38:36 +00:00
2013-09-13 14:32:57 +00:00
Add the gem to your Gemfile.
2011-11-04 15:51:22 +00:00
2013-09-13 14:32:57 +00:00
```ruby
gem "rack-livereload", :group => :development
```
2011-11-04 15:51:22 +00:00
2013-09-13 14:32:57 +00:00
Then add the middleware to your Rails middleware stack by editing your `config/environments/development.rb` .
2012-05-30 16:51:24 +00:00
2013-09-13 14:32:57 +00:00
```ruby
# config/environments/development.rb
2011-11-04 15:51:22 +00:00
MyApp::Application.configure do
2013-09-13 14:32:57 +00:00
# Add Rack::LiveReload to the bottom of the middleware stack with the default options.
config.middleware.use Rack::LiveReload
# ...
2011-11-04 15:51:22 +00:00
end
```
2013-09-13 14:32:57 +00:00
#### Tweaking the options
```ruby
# Specifying Rack::LiveReload options.
config.middleware.use(Rack::LiveReload,
:min_delay => 500, # default 1000
:max_delay => 10_000, # default 60_000
:live_reload_port => 56789, # default 35729
:host => 'myhost.cool.wow',
:ignore => [ %r{dont/modify\.html$} ]
)
```
In addition, Rack::LiveReload's position within middleware stack can be specified by inserting it relative to an exsiting middleware via `insert_before` or `insert_after` . See the [Rails on Rack: Adding a Middleware ](http://guides.rubyonrails.org/rails_on_rack.html#adding-a-middleware ) section for more detail.
### Sinatra / config.ru
2011-11-04 15:51:22 +00:00
``` ruby
require 'rack-livereload'
use Rack::LiveReload
2011-11-07 14:06:40 +00:00
# ...or...
use Rack::LiveReload, :min_delay => 500, ...
2011-11-04 15:51:22 +00:00
```
## How it works
2011-11-08 14:48:34 +00:00
The necessary `script` tag to bring in a copy of [livereload.js ](https://github.com/livereload/livereload-js ) is
2012-12-05 14:04:42 +00:00
injected right after the opening `head` tag in any `text/html` pages that come through. The `script` tag is built in
2011-11-04 15:51:22 +00:00
such a way that the `HTTP_HOST` is used as the LiveReload host, so you can connect from external machines (say, to
`mycomputer:3000` instead of `localhost:3000` ) and as long as the LiveReload port is accessible from the external machine,
you'll connect and be LiveReloading away!
2011-11-08 14:48:34 +00:00
### Which LiveReload script does it use?
* If you've got a LiveReload watcher running on the same machine as the app that responds
to `http://localhost:35729/livereload.js` , that gets used, with the hostname being changed when
injected into the HTML page.
* If you don't, the copy vendored with rack-livereload is used.
* You can force the use of either one (and save on the cost of checking to see if that file
is available) with the middleware option `:source => :vendored` or `:source => :livereload` .
2011-11-17 20:44:48 +00:00
### How about non-WebSocket-enabled browsers?
2011-11-17 20:48:33 +00:00
For browsers that don't support WebSockets, but do support Flash, [web-socket-js ](https://github.com/gimite/web-socket-js )
2011-11-17 20:44:48 +00:00
is loaded. By default, this is done transparently, so you'll get a copy of swfobject.js and web_socket.js loaded even if
your browser doesn't need it. The SWF WebSocket implementor won't be loaded unless your browser has no native
WebSockets support or if you force it in the middleware stack:
``` ruby
use Rack::LiveReload, :force_swf => true
```
If you don't want any of the web-sockets-js code included at all, use the `no_swf` option:
``` ruby
use Rack::LiveReload, :no_swf => true
```
Once more browsers support WebSockets than don't, this option will be reversed and you'll have
to explicitly include the Flash shim.