Extend cache busting to allow for path manipulation

This commit is contained in:
Nathaniel Bibler 2011-03-27 22:35:49 -04:00 committed by Mattias Pfeiffer
parent 9b5d5b72a1
commit 21928907e4
7 changed files with 60 additions and 5 deletions

View File

@ -71,8 +71,10 @@ module Compass
end
# When called with a block, defines the cache buster strategy to be used.
# The block must return nil or a string that can be appended to a url as a query parameter.
# The returned string must not include the starting '?'.
# If the block returns nil or a string, then it is appended to the url as a query parameter.
# In this case, the returned string must not include the starting '?'.
# The block may also return a full path (i.e. /images/grid-BUSTED.png) and it
# will replace the original path and query string with the busted path returned.
# The block will be passed the root-relative url of the asset.
# If the block accepts two arguments, it will also be passed a File object
# that points to the asset on disk -- which may or may not exist.

View File

@ -91,9 +91,7 @@ module Compass::SassExtensions::Functions::Urls
if cache_buster.is_a?(Sass::Script::String)
path += "?#{cache_buster.value}"
else
if buster = compute_cache_buster(path, real_path)
path += "?#{buster}"
end
path = cache_busted_path(path, real_path)
end
end
@ -137,6 +135,17 @@ module Compass::SassExtensions::Functions::Urls
end
end
def cache_busted_path(path, real_path)
cache_buster = compute_cache_buster(path, real_path)
if cache_buster.nil?
path
elsif cache_buster =~ %r{/}
cache_buster
else
"%s?%s" % [path, cache_buster]
end
end
def compute_cache_buster(path, real_path)
if Compass.configuration.asset_cache_buster
args = [path]

View File

@ -71,6 +71,17 @@ class CompassTest < Test::Unit::TestCase
end
end
def test_busted_image_urls
within_project('busted_image_urls') do |proj|
each_css_file(proj.css_path) do |css_file|
assert_no_errors css_file, 'busted_image_urls'
end
each_sass_file do |sass_file|
assert_renders_correctly sass_file
end
end
end
def test_image_urls
within_project('image_urls') do |proj|
each_css_file(proj.css_path) do |css_file|

View File

@ -0,0 +1,20 @@
# Require any additional compass plugins here.
project_type = :stand_alone
css_dir = "tmp"
sass_dir = "sass"
images_dir = "images"
output_style = :compact
# To enable relative image paths using the images_url() function:
# http_images_path = :relative
http_images_path = "/images"
line_comments = false
asset_cache_buster do |path, file|
pathname = Pathname.new(path)
"%s/%s-BUSTED%s" % [pathname.dirname, pathname.basename(pathname.extname), pathname.extname]
end
asset_host do |path|
"http://assets%d.example.com" % (path.size % 4)
end

View File

@ -0,0 +1,5 @@
.showgrid { background-image: url('http://assets0.example.com/images/grid-BUSTED.png'); }
.inlinegrid { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAUEAYAAACv1qP4AAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAZ0lEQVRYw+3QwQ2AIBAFUTEUwI3+uzN7gDscsIgxEuO8An52J11X73OudfxMraXkzHfO3Y98nQEhA0IGhAwIGRAyIGRAyICQASEDQgaEDAgZEDIgZEDIgJABoZzSGK3tPuN9ERFP7Nw4fg+c5g8V1wAAAABJRU5ErkJggg=='); }
.no-buster { background-image: url('http://assets0.example.com/images/grid.png'); }

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

View File

@ -0,0 +1,8 @@
.showgrid
background-image: image-url(unquote("grid.png"))
.inlinegrid
background-image: inline-image(unquote("grid.png"))
.no-buster
background-image: image-url("grid.png", $only-path: false, $cache-buster: false)