From 9b050883e19f46969248f5bf7b4749d54caaa3c5 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Tue, 17 Nov 2009 15:52:28 -0800 Subject: [PATCH] make cache buster more robust --- lib/middleman/features/cache_buster.rb | 20 +++++++++++++------- spec/cache_buster_off_spec.rb | 25 ++++++++++++------------- spec/cache_buster_on_spec.rb | 19 +++++++++---------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index fcffe0ef..2a7ab84c 100755 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -1,14 +1,18 @@ class Middleman::Base after_feature_init do ::Compass.configuration do |config| - config.asset_cache_buster do |path, real_path| - real_path = real_path.path if real_path.is_a? File - real_path = real_path.gsub(File.join(self.root, self.build_dir), self.public) - if File.readable?(real_path) - File.mtime(real_path).strftime("%s") - else - $stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}" + if self.respond_to?(:cache_buster?) && self.cache_buster? + config.asset_cache_buster do |path, real_path| + real_path = real_path.path if real_path.is_a? File + real_path = real_path.gsub(File.join(self.root, self.build_dir), self.public) + if File.readable?(real_path) + File.mtime(real_path).strftime("%s") + else + $stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}" + end end + else + config.asset_cache_buster { false } end end @@ -21,6 +25,8 @@ class << Middleman::Base def asset_url(path, prefix="", request=nil) 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)) http_path else diff --git a/spec/cache_buster_off_spec.rb b/spec/cache_buster_off_spec.rb index ac612f18..e9652bf0 100755 --- a/spec/cache_buster_off_spec.rb +++ b/spec/cache_buster_off_spec.rb @@ -1,21 +1,20 @@ 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 - browser = Rack::Test::Session.new(Rack::MockSession.new(@base.new)) - browser.get("/stylesheets/relative_assets.css") - browser.last_response.body.should_not include("?") + @browser.get("/stylesheets/relative_assets.css") + @browser.last_response.body.count("?").should == 0 end 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.last_response.body.count("?").should == 0 + @browser.get("/cache-buster.html") + @browser.last_response.body.count("?").should == 0 end end \ No newline at end of file diff --git a/spec/cache_buster_on_spec.rb b/spec/cache_buster_on_spec.rb index 847bb679..f84479af 100755 --- a/spec/cache_buster_on_spec.rb +++ b/spec/cache_buster_on_spec.rb @@ -1,21 +1,20 @@ 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.enable :cache_buster + before do + base = ::Middleman::Base + base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample") + base.enable :cache_buster + @browser = Rack::Test::Session.new(Rack::MockSession.new(base.new)) end 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.last_response.body.should include("?") + @browser.get("/stylesheets/relative_assets.css") + @browser.last_response.body.should include("?") end 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.last_response.body.count("?").should == 0 + @browser.get("/cache-buster.html") + @browser.last_response.body.count("?").should == 0 end end \ No newline at end of file