From 21928907e4ae8f739cc9baf006d580ce7d2aaf07 Mon Sep 17 00:00:00 2001 From: Nathaniel Bibler Date: Sun, 27 Mar 2011 22:35:49 -0400 Subject: [PATCH 1/6] Extend cache busting to allow for path manipulation --- lib/compass/configuration/data.rb | 6 ++++-- lib/compass/sass_extensions/functions/urls.rb | 15 ++++++++++--- test/compass_test.rb | 11 ++++++++++ .../stylesheets/busted_image_urls/config.rb | 20 ++++++++++++++++++ .../busted_image_urls/css/screen.css | 5 +++++ .../busted_image_urls/images/grid.png | Bin 0 -> 199 bytes .../busted_image_urls/sass/screen.sass | 8 +++++++ 7 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/stylesheets/busted_image_urls/config.rb create mode 100644 test/fixtures/stylesheets/busted_image_urls/css/screen.css create mode 100644 test/fixtures/stylesheets/busted_image_urls/images/grid.png create mode 100644 test/fixtures/stylesheets/busted_image_urls/sass/screen.sass diff --git a/lib/compass/configuration/data.rb b/lib/compass/configuration/data.rb index 9cde724d..0173fa93 100644 --- a/lib/compass/configuration/data.rb +++ b/lib/compass/configuration/data.rb @@ -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. diff --git a/lib/compass/sass_extensions/functions/urls.rb b/lib/compass/sass_extensions/functions/urls.rb index d140c8a5..5fce4b69 100644 --- a/lib/compass/sass_extensions/functions/urls.rb +++ b/lib/compass/sass_extensions/functions/urls.rb @@ -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] diff --git a/test/compass_test.rb b/test/compass_test.rb index 26491328..69cea09c 100644 --- a/test/compass_test.rb +++ b/test/compass_test.rb @@ -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| diff --git a/test/fixtures/stylesheets/busted_image_urls/config.rb b/test/fixtures/stylesheets/busted_image_urls/config.rb new file mode 100644 index 00000000..9961c9bd --- /dev/null +++ b/test/fixtures/stylesheets/busted_image_urls/config.rb @@ -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 diff --git a/test/fixtures/stylesheets/busted_image_urls/css/screen.css b/test/fixtures/stylesheets/busted_image_urls/css/screen.css new file mode 100644 index 00000000..9935c20f --- /dev/null +++ b/test/fixtures/stylesheets/busted_image_urls/css/screen.css @@ -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'); } diff --git a/test/fixtures/stylesheets/busted_image_urls/images/grid.png b/test/fixtures/stylesheets/busted_image_urls/images/grid.png new file mode 100644 index 0000000000000000000000000000000000000000..76aaa851a17551b07c608797105bf6e4ba204148 GIT binary patch literal 199 zcmeAS@N?(olHy`uVBq!ia0vp^8bB-}zy>7OUt9bGNUZ}$lIVGz#3>Ma-jF$ZsY0(YncrlhC+|`Fx8c*$A-Tz zURV0ZXYJA_XUfmr?XQ{3sL1TZ*22RfDPW|K;&7s20;a&iIhQ2XzO{Q?D=6sy=8i=j W|D0$1qSt{|GI+ZBxvX Date: Wed, 4 May 2011 11:37:58 +0200 Subject: [PATCH 2/6] Make asset_cache_buster return a hash with path and query, while still maintaining support for cache busting via query string if a string is returned. --- lib/compass/sass_extensions/functions/urls.rb | 14 ++++++++++---- .../stylesheets/busted_image_urls/config.rb | 11 ++++++++++- .../stylesheets/busted_image_urls/css/screen.css | 4 ++++ .../stylesheets/busted_image_urls/images/feed.png | Bin 0 -> 691 bytes .../busted_image_urls/images/flags/dk.png | Bin 0 -> 808 bytes .../busted_image_urls/sass/screen.sass | 6 ++++++ 6 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/stylesheets/busted_image_urls/images/feed.png create mode 100644 test/fixtures/stylesheets/busted_image_urls/images/flags/dk.png diff --git a/lib/compass/sass_extensions/functions/urls.rb b/lib/compass/sass_extensions/functions/urls.rb index 5fce4b69..50ffab52 100644 --- a/lib/compass/sass_extensions/functions/urls.rb +++ b/lib/compass/sass_extensions/functions/urls.rb @@ -138,11 +138,17 @@ module Compass::SassExtensions::Functions::Urls 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 + return path + elsif cache_buster.is_a?(String) + cache_buster = {:query => cache_buster} else - "%s?%s" % [path, cache_buster] + path = cache_buster[:path] if cache_buster[:path] + end + + if cache_buster[:query] + "%s?%s" % [path, cache_buster[:query]] + else + path end end diff --git a/test/fixtures/stylesheets/busted_image_urls/config.rb b/test/fixtures/stylesheets/busted_image_urls/config.rb index 9961c9bd..13821b8e 100644 --- a/test/fixtures/stylesheets/busted_image_urls/config.rb +++ b/test/fixtures/stylesheets/busted_image_urls/config.rb @@ -11,7 +11,16 @@ 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] + + case pathname.basename(pathname.extname).to_s + when "grid" + new_path = "%s/%s-BUSTED%s" % [pathname.dirname, pathname.basename(pathname.extname), pathname.extname] + {:path => new_path, :query => nil} + when "feed" + "query_string" + when "dk" + {:query => "query_string"} + end end diff --git a/test/fixtures/stylesheets/busted_image_urls/css/screen.css b/test/fixtures/stylesheets/busted_image_urls/css/screen.css index 9935c20f..ab36dbc7 100644 --- a/test/fixtures/stylesheets/busted_image_urls/css/screen.css +++ b/test/fixtures/stylesheets/busted_image_urls/css/screen.css @@ -3,3 +3,7 @@ .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'); } + +.feed { background-image: url('http://assets0.example.com/images/feed.png?query_string'); } + +.dk { background-image: url('http://assets0.example.com/images/flags/dk.png?query_string'); } diff --git a/test/fixtures/stylesheets/busted_image_urls/images/feed.png b/test/fixtures/stylesheets/busted_image_urls/images/feed.png new file mode 100644 index 0000000000000000000000000000000000000000..315c4f4fa62cb720326ba3f54259666ba3999e42 GIT binary patch literal 691 zcmV;k0!;mhP)bpQb1=l6TxbDZwj&S={?7%qx-u`rsG(Zp`-rh=e^=%((1yvsuf5d=&62Zj)Y zH&JviNS_F4_Hj|T(1j4$p-!}kixP9&dB4uv^MveG?dGf%sUCoc2!IFxD6wHRA2^dX zXRVk!-qSfk(jcaUKn#RP48(whfPlJUpApdrA!TQi_4D+fVoM;3I0gZ8{=Xv~Po;geVA+Em9@0Wq2 zr>OTZEGR05L=gf1T;ucCxq6Q6EgJiH@@-lVaAlQyw`jIF^c=&IVnj|95hHbE_cnt| zTzZQ?F4Ne@(bH(~&3nM%m)I@ID{@jJ2qZPjr)jhpe9hViOwH5k&|T#EmmL3(vHeUQ zq^!t^Al6JD;=mHq^Bg?J-8-zG2Od7gZbknG;K9czYjPqG*xjPo0k(c4%lPXTpw(qq z@aGMnxtFS(np+2kC} z7P02O874ZkJH$v#nCUVx$({yDN`IX@o2wyvTD#e`qN`_w5<}$3F+_ra@ocD)4hISV`@ zi-EKU7`vU!wgU;46*#5?X|QgFuOHGP85kJVJzX3_G|uM+S^F^u^33^O#?8{@(mEwc zI`4qv1BERYo`z`_cf=?%IXbcgO=DS7k;8v9dV)hwOvJzHH%3gIoSbvqpOo#MeO~y7 zG9SYWVT(u|1_j}nOQ%@Po_qKH)8otUxE_jKa%XZ)q+T^c!>5;$f?X4qblk9clBgf# zb}?mIifmhs?1Wj1JQ*fUnx;ENEqPX_&Jtfv!J_HbFD8Yo{#GsFIC-YWj7E`}4xJrA zT`Hby_ZVHBa$jR>-|pCcd3lzFPhZ-LXd3^{y?!S|{tLu5oe`}`p2UfOqkzb!EKM(_kpK6I~L`iZ{YGO&MZVHfK zFfuT()HO8JH8cw`GO{u>ure{zH88g_Fj(fW`zcHVNTn4-7f^$tfvy2igJFn)nU$%b zm7xVh1K&H2ohTY|^HVa@DsgM5-@JhpsKE&2B%jRW)V$(UhK!Pu0xNy})I7cN%|Ed8}R@D literal 0 HcmV?d00001 diff --git a/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass b/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass index e16697bc..8f844f2a 100644 --- a/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass +++ b/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass @@ -6,3 +6,9 @@ .no-buster background-image: image-url("grid.png", $only-path: false, $cache-buster: false) + +.feed + background-image: image-url(unquote("feed.png")) + +.dk + background-image: image-url(unquote("flags/dk.png")) \ No newline at end of file From 8661c481cf21535729fcfc21af911865091d5d69 Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Wed, 4 May 2011 11:43:22 +0200 Subject: [PATCH 3/6] Fix comments for asset_cache_buster --- lib/compass/configuration/data.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compass/configuration/data.rb b/lib/compass/configuration/data.rb index 0173fa93..ddc2de3a 100644 --- a/lib/compass/configuration/data.rb +++ b/lib/compass/configuration/data.rb @@ -73,8 +73,8 @@ module Compass # When called with a block, defines the cache buster strategy to be used. # 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 may also return a hash with :path and/or :query values and it + # will replace the original path and query string with the busted values 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. From d4422d108e6259103a53cc95f7cdcfbb45758721 Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Thu, 5 May 2011 10:06:48 +0200 Subject: [PATCH 4/6] Adding changelog entry --- doc-src/content/CHANGELOG-v0-11-beta.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/doc-src/content/CHANGELOG-v0-11-beta.markdown b/doc-src/content/CHANGELOG-v0-11-beta.markdown index 127f0f24..17fc5769 100644 --- a/doc-src/content/CHANGELOG-v0-11-beta.markdown +++ b/doc-src/content/CHANGELOG-v0-11-beta.markdown @@ -10,6 +10,7 @@ layout: article * Created an official API to add configuration options to compass. [More information](/help/tutorials/extending/). +* Enhanced cache busting by allowing path manipulation 0.11.beta.7 (04/16/2011) ------------------------ From 6b3ca3901cec07776a6c21bedd4989d268267017 Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Thu, 5 May 2011 10:14:21 +0200 Subject: [PATCH 5/6] Adding note about path based asset cache busting in to documentation --- .../configuration-reference.markdown | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/doc-src/content/help/tutorials/configuration-reference.markdown b/doc-src/content/help/tutorials/configuration-reference.markdown index af6d6ea7..3a966856 100644 --- a/doc-src/content/help/tutorials/configuration-reference.markdown +++ b/doc-src/content/help/tutorials/configuration-reference.markdown @@ -308,9 +308,12 @@ the asset host configuration is ignored. --- **`asset_cache_buster`** – Pass this function a block of code that 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 `?`. The block will be passed the root-relative url of the asset. +cache buster strategy to be used. The block must return nil, a string or a hash. +If the returned value is a hash the values of :path and/or :query is used to generate +a cache busted path to the asset. If a string value is returned, it is added as a query string. +The returned values for query strings must not include the starting `?`. + +The block will be passed the root-relative url of the asset. If the block accepts two arguments, it will also be passed a path that points to the asset on disk — which may or may not exist. @@ -324,6 +327,18 @@ that points to the asset on disk — which may or may not exist. end end +Busting the cache via path: + + asset_cache_buster do |path, real_path| + if File.exists?(real_path) + pathname = Pathname.new(path) + modified_time = File.mtime(real_path).strftime("%s") + new_path = "%s/%s-%s%s" % [pathname.dirname, pathname.basename(pathname.extname), modified_time, pathname.extname] + + {:path => new_path, :query => nil} + end + end + To disable the asset cache buster: asset_cache_buster :none From 223258c2032d089100269fbff6281c971bad8aa1 Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Thu, 5 May 2011 10:19:19 +0200 Subject: [PATCH 6/6] Removing unneeded 'unquote' from tests --- .../stylesheets/busted_image_urls/sass/screen.sass | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass b/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass index 8f844f2a..b469b120 100644 --- a/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass +++ b/test/fixtures/stylesheets/busted_image_urls/sass/screen.sass @@ -1,14 +1,14 @@ .showgrid - background-image: image-url(unquote("grid.png")) + background-image: image-url("grid.png") .inlinegrid - background-image: inline-image(unquote("grid.png")) + background-image: inline-image("grid.png") .no-buster background-image: image-url("grid.png", $only-path: false, $cache-buster: false) .feed - background-image: image-url(unquote("feed.png")) + background-image: image-url("feed.png") .dk - background-image: image-url(unquote("flags/dk.png")) \ No newline at end of file + background-image: image-url("flags/dk.png") \ No newline at end of file