Is there a way to do file rev cache busting in SASS? It appears to be possible using compass per this answer here: SASS Image CSS Cache Busting (via Compass)
but I haven't found any way to do this just using SASS. Is there any way to have SASS get file modification info from the filesystem, and append to an image path?
And I'd prefer to not append query strings- rather, I think this is a better methodology.
Thanks Dawn for chiming in after all this time! I've since figured this out, but forgot I posted here about it.
I have a custom rb file that I reference when I run sass via the command line - like so:
sass --update sass:css -r file_mod.rb
in file_mod.rb, I have the following ruby function which does the trick:
require 'sass'
module GETMODINT
def file_url(staticFilePath,staticHost,filePath)
assert_type filePath, :String
filePath = filePath.value #get string value of literal
staticFilePath = staticFilePath.value
staticHost = staticHost.value
modtime = File.mtime(filePath).to_i
#Sass::Script::Number.new(modtime)
fileBaseName = File.basename filePath, '.*'
fileDir = File.dirname(filePath).sub(staticFilePath,'')
fileExt = File.extname(filePath)
path = "url('#{staticHost}#{fileDir}/#{fileBaseName}.#{modtime}#{fileExt}')"
return Sass::Script::String.new(path)
end
Sass::Script::Functions.declare :modInt, [:filePath]
end
module Sass::Script::Functions
include GETMODINT
end
Then, in a sass mixin, I simply reference the file_url function, passing it the parameters it needs to build the result:
@mixin backgroundImage($path, $position:0 0, $repeat:no-repeat) {
background: {
image:file_url($staticFilePath,$staticHost,$path);
position:$position;
repeat:$repeat;
}
}
In my case, I'm using it to construct a css background image path. Should be easily modified to suit other purposes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.