From 20ef5cc1f3497c56d70dbadc44e39efb08870672 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 1 Dec 2011 07:25:02 -0500 Subject: [PATCH] add the ability to ignore certain urls that may match --- README.md | 3 ++- lib/rack/livereload.rb | 46 +++++++++++++++++++----------------- spec/rack/livereload_spec.rb | 9 +++++++ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 6f12355..56988ef 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ MyApp::Application.configure do :min_delay => 500, :max_delay => 10000, :port => 56789, - :host => 'myhost.cool.wow' + :host => 'myhost.cool.wow', + :ignore => [ %r{dont/modify\.html$} ] ) end ``` diff --git a/lib/rack/livereload.rb b/lib/rack/livereload.rb index 89d1a52..85605a9 100644 --- a/lib/rack/livereload.rb +++ b/lib/rack/livereload.rb @@ -48,35 +48,37 @@ module Rack else status, headers, body = @app.call(env) - case headers['Content-Type'] - when %r{text/html} - content_length = 0 + if !@options[:ignore] || !@options[:ignore].any? { |filter| path[filter] } + case headers['Content-Type'] + when %r{text/html} + content_length = 0 - body.each do |line| - if !headers['X-Rack-LiveReload'] && line[''] - host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '') + body.each do |line| + if !headers['X-Rack-LiveReload'] && line[''] + host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '') - if use_vendored? - src = LIVERELOAD_JS_PATH.dup + "?host=#{host_to_use}" - else - src = LIVERELOAD_LOCAL_URI.dup.gsub('localhost', host_to_use) + '?' + if use_vendored? + src = LIVERELOAD_JS_PATH.dup + "?host=#{host_to_use}" + else + src = LIVERELOAD_LOCAL_URI.dup.gsub('localhost', host_to_use) + '?' + end + + src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay] + src << "&maxdelay=#{@options[:max_delay]}" if @options[:max_delay] + src << "&port=#{@options[:port]}" if @options[:port] + + template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__))) + + line.gsub!('', %{#{template.result(binding)}}) + + headers["X-Rack-LiveReload"] = '1' end - src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay] - src << "&maxdelay=#{@options[:max_delay]}" if @options[:max_delay] - src << "&port=#{@options[:port]}" if @options[:port] - - template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__))) - - line.gsub!('', %{#{template.result(binding)}}) - - headers["X-Rack-LiveReload"] = '1' + content_length += line.length end - content_length += line.length + headers['Content-Length'] = content_length.to_s end - - headers['Content-Length'] = content_length.to_s end [ status, headers, body ] diff --git a/spec/rack/livereload_spec.rb b/spec/rack/livereload_spec.rb index fb530b1..ee0cbff 100644 --- a/spec/rack/livereload_spec.rb +++ b/spec/rack/livereload_spec.rb @@ -132,6 +132,15 @@ describe Rack::LiveReload do body.should include('localhost') end end + + context 'ignored' do + let(:options) { { :ignore => [ %r{file} ] } } + let(:env) { { 'PATH_INFO' => 'this/file' } } + + it 'should have no change' do + body.should_not include('script') + end + end end context '/__rack/livereload.js' do