potential fix for turbolinks load order problem

This commit is contained in:
John Bintz 2012-12-03 14:00:45 -05:00
parent d1b9a6ef7b
commit 7a6a51cb62
3 changed files with 22 additions and 3 deletions

View File

@ -61,7 +61,7 @@ module Rack
content_length = 0
new_body.each do |line|
if !headers['X-Rack-LiveReload'] && line['</head>']
if !headers['X-Rack-LiveReload'] && (line['</head>'] || line['<script'])
host_to_use = (@options[:host] || env['HTTP_HOST'] || 'localhost').gsub(%r{:.*}, '')
if use_vendored?
@ -76,7 +76,11 @@ module Rack
template = ERB.new(::File.read(::File.expand_path('../../../skel/livereload.html.erb', __FILE__)))
line.gsub!('</head>', %{#{template.result(binding)}</head>})
if line['<script']
line.sub!('<script', %{#{template.result(binding)}<script})
else
line.gsub!('</head>', %{#{template.result(binding)}</head>})
end
headers["X-Rack-LiveReload"] = '1'
end

View File

@ -32,6 +32,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "guard-cucumber"
s.add_development_dependency "guard-livereload"
s.add_development_dependency "webmock"
s.add_development_dependency "nokogiri"
s.add_development_dependency 'appraisal', '~> 0.4'
s.add_runtime_dependency "rack"
end

View File

@ -1,4 +1,5 @@
require 'spec_helper'
require 'nokogiri'
describe Rack::LiveReload do
let(:middleware) { described_class.new(app, options) }
@ -67,7 +68,7 @@ describe Rack::LiveReload do
context 'text/html' do
before do
app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ '<head></head>' ] ])
app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ page_html ] ])
middleware.stubs(:use_vendored?).returns(true)
end
@ -78,6 +79,8 @@ describe Rack::LiveReload do
let(:body) { ret.last.join }
let(:length) { ret[1]['Content-Length'] }
let(:page_html) { '<head></head>' }
context 'vendored' do
it 'should add the vendored livereload js script tag' do
body.should include("script")
@ -92,6 +95,17 @@ describe Rack::LiveReload do
end
end
context 'before script tags' do
let(:page_html) { '<head><script type="text/javascript" insert="before"></script></head>' }
let(:body_dom) { Nokogiri::XML(body) }
it 'should add the livereload js script tag before all other script tags' do
body_dom.at_css("script:eq(4)")[:src].should include(described_class::LIVERELOAD_JS_PATH)
body_dom.at_css("script:last-child")[:insert].should == "before"
end
end
context 'not vendored' do
before do
middleware.stubs(:use_vendored?).returns(false)