make cache buster more robust

This commit is contained in:
tdreyno 2009-11-17 15:52:28 -08:00
parent 83d3456837
commit 9b050883e1
3 changed files with 34 additions and 30 deletions

View file

@ -1,14 +1,18 @@
class Middleman::Base class Middleman::Base
after_feature_init do after_feature_init do
::Compass.configuration do |config| ::Compass.configuration do |config|
config.asset_cache_buster do |path, real_path| if self.respond_to?(:cache_buster?) && self.cache_buster?
real_path = real_path.path if real_path.is_a? File config.asset_cache_buster do |path, real_path|
real_path = real_path.gsub(File.join(self.root, self.build_dir), self.public) real_path = real_path.path if real_path.is_a? File
if File.readable?(real_path) real_path = real_path.gsub(File.join(self.root, self.build_dir), self.public)
File.mtime(real_path).strftime("%s") if File.readable?(real_path)
else File.mtime(real_path).strftime("%s")
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}" else
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
end
end end
else
config.asset_cache_buster { false }
end end
end end
@ -21,6 +25,8 @@ class << Middleman::Base
def asset_url(path, prefix="", request=nil) def asset_url(path, prefix="", request=nil)
http_path = pre_cache_buster_asset_url(path, prefix, request) http_path = pre_cache_buster_asset_url(path, prefix, request)
return http_path unless self.respond_to?(:cache_buster?) && self.cache_buster?
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path)) if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
http_path http_path
else else

View file

@ -1,21 +1,20 @@
require File.join(File.dirname(__FILE__), "spec_helper") require File.join(File.dirname(__FILE__), "spec_helper")
describe "Cache Buster Feature in CSS" do
before :each do
@base = ::Middleman::Base
@base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
@base.disable :cache_buster
end
describe "Cache Buster Feature in CSS" do
before do
base = ::Middleman::Base
base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
base.disable :cache_buster
@browser = Rack::Test::Session.new(Rack::MockSession.new(base.new))
end
it "should not append query string in CSS if off" do it "should not append query string in CSS if off" do
browser = Rack::Test::Session.new(Rack::MockSession.new(@base.new)) @browser.get("/stylesheets/relative_assets.css")
browser.get("/stylesheets/relative_assets.css") @browser.last_response.body.count("?").should == 0
browser.last_response.body.should_not include("?")
end end
it "should not append query string in HTML if off" do it "should not append query string in HTML if off" do
browser = Rack::Test::Session.new(Rack::MockSession.new(@base.new)) @browser.get("/cache-buster.html")
browser.get("/cache-buster.html") @browser.last_response.body.count("?").should == 0
browser.last_response.body.count("?").should == 0
end end
end end

View file

@ -1,21 +1,20 @@
require File.join(File.dirname(__FILE__), "spec_helper") require File.join(File.dirname(__FILE__), "spec_helper")
describe "Cache Buster Feature in CSS" do describe "Cache Buster Feature in CSS" do
before :each do before do
@base = ::Middleman::Base base = ::Middleman::Base
@base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample") base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
@base.enable :cache_buster base.enable :cache_buster
@browser = Rack::Test::Session.new(Rack::MockSession.new(base.new))
end end
it "should append query string in CSS if on" do it "should append query string in CSS if on" do
browser = Rack::Test::Session.new(Rack::MockSession.new(@base.new)) @browser.get("/stylesheets/relative_assets.css")
browser.get("/stylesheets/relative_assets.css") @browser.last_response.body.should include("?")
browser.last_response.body.should include("?")
end end
it "should not append query string in HTML if on IN DEVELOPMENT" do it "should not append query string in HTML if on IN DEVELOPMENT" do
browser = Rack::Test::Session.new(Rack::MockSession.new(@base.new)) @browser.get("/cache-buster.html")
browser.get("/cache-buster.html") @browser.last_response.body.count("?").should == 0
browser.last_response.body.count("?").should == 0
end end
end end