我一直在使用asset packager,最后我编辑了插件的compress_css
方法来解决这个问题。我基本上只是正则表达式中的CSS图像,插入当前时间戳记:
timestamp = Time.now.to_s.gsub(/\D/, '')
source.gsub!(/url\((['"])(.+)(['"])\)/) do
open, file, close = $1, $2, $3
if file =~ /.\.(ico|css|js|gif|jpe?g|png)/
"url(#{open}#{file}?#{timestamp}#{close})"
else
"url(#{open}#{file}#{close})"
end
end
这样一来,每当我部署,压缩CSS图像包含附加时间戳。这种方法的缺点是每个图像都没有自己的时间戳,所以每次部署新的css时,所有的css图像都是“过期”的。总比没有,除非你经常部署CSS。
source.gsub!(/url\((['"]*)(.+)(['"]*)\)/) do
open, file, close = $1, $2, $3
css_dir = File.join(RAILS_ROOT,"public/stylesheets")
timestamp = ''
FileUtils.cd(css_dir) do
if file =~ /^\// # absolute path
f = File.new(RAILS_ROOT + "/public" + file)
else # relative path
f = File.new(file)
end
timestamp = f.mtime.to_i.to_s
end
if file =~ /.\.(ico|css|js|gif|jpe?g|png)/
"url(#{open}#{file}?#{timestamp}#{close})"
else
"url(#{open}#{file}#{close})"
end
end
(!有可能写这个更优雅的方式,我的红宝石扒仍然疲软) :
来源
2009-09-08 14:21:39
Ben
看到我的回答下面,我认为现在最好的解决方案,现在它使用Jammit,包装,它支持这个开箱即用 – brad 2010-08-27 14:33:45