如何从Compass生成的精灵图像文件名中删除哈希?

时间:2012-02-07 20:09:22

标签: css sass compass-sass

指南针使用chunky_png渲染精灵。它在文件末尾添加一个哈希,以强制缓存下载新的图像精灵。有没有办法让这个缓存破坏?

4 个答案:

答案 0 :(得分:23)

不幸的是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文件。在样式表中,对精灵的所有引用现在都指向没有哈希的版本。

请务必保持文件的哈希值,以便Compass优化编译时间。

答案 1 :(得分:22)

将config.rb中的asset_cache_buster :none设置为documented in their configuration reference

答案 2 :(得分:4)

可以在另一个similar question中找到更好的解决方案。

这更好,因为:

  1. 脚本在生成精灵之前更改名称 - 而不是之后。
  2. 由于第1点,因此无需更改.css自动生成的文件。它从一开始就以正确的名称生成。
  3. 接受的解决方案使用哈希生成精灵的cp(副本),并且它作为重复保留在文件系统/ repo中,这非常糟糕。此外,它仍然被视为使用本地存储库更改,因此您提交了两个相同的文件。解决方案可以mv更改生成的哈希文件名以清除它,但在这种情况下,每次在.scss文件中使用时都会生成精灵,所以情况更糟。

答案 3 :(得分:1)

我没有使用精灵进行测试,但这适用于replace-text-with-dimensions,例如:

config.rb:

# disable asset cache buster
asset_cache_buster do |http_path, real_path|
  nil
end

找到The compass configuration file at caring.com