我一直在使用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