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
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

View file

@ -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

View file

@ -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