Compass 使用 chunky_png 渲染 Sprite 。它在文件末尾添加一个哈希值以强制缓存下载新的图像 Sprite 。有没有办法关闭此缓存破坏?
最佳答案
不幸的是,asset_cache_buster :none
选项不会禁用将哈希添加到文件名的末尾。
就像我之前写的(法语),Compass 没有办法禁用缓存哈希破坏器,但我建议使用 a solution。
在您的配置文件(例如 config.rb
)中添加以下行:
# Make a copy of sprites with a name that has no uniqueness of the hash.
on_sprite_saved do |filename|
if File.exists?(filename)
FileUtils.cp filename, filename.gsub(%r{-s[a-z0-9]{10}\.png$}, '.png')
end
end
# Replace in stylesheets generated references to sprites
# by their counterparts without the hash uniqueness.
on_stylesheet_saved do |filename|
if File.exists?(filename)
css = File.read filename
File.open(filename, 'w+') do |f|
f << css.gsub(%r{-s[a-z0-9]{10}\.png}, '.png')
end
end
end
现在,使用 compass clean
删除生成的文件并使用 compass compile
重新开始编译。
例如,您获得了一个images/icons-scb1e5456d5.png
文件和一个images/icons.png
文件。在样式表中,所有对 Sprite 的引用现在都指向没有散列的版本。
确保文件具有提供的哈希值,以优化 Compass 的编译时间。
关于css - 如何从 Compass 生成的 Sprite 图像文件名中删除散列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183133/