How to remove the hash from Compass's generate

2019-01-13 22:22发布

Compass uses chunky_png to render the sprites. It adds a hash to the end of the file to force caches to download the new image sprites. Is there a way to turn this cache busting off?

4条回答
\"骚年 ilove
2楼-- · 2019-01-13 22:32

Unfortunately asset_cache_buster :none option does not disable adding the hash to the end of the filename.

Like I wrote few time ago (in french), Compass has no way to disable the cache hash buster, but I propose a solution.
In your configuration file (eg config.rb) add the following lines:

# 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

Now, uses compass cleanto remove generated files and restarts a compilation with compass compile.
You obtain, for example, a images/icons-scb1e5456d5.png file and a images/icons.png file. In the stylesheets, all references to the sprites now point to the version without hash.

Be sure to keep the file has a hash provided to optimize compile times by Compass.

查看更多
叼着烟拽天下
3楼-- · 2019-01-13 22:33

I haven't tested with sprites, but this works with replace-text-with-dimensions, for example:

config.rb:

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

Found at The compass configuration file at caring.com

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-13 22:43

Set asset_cache_buster :none in your config.rb as documented in their configuration reference

查看更多
干净又极端
5楼-- · 2019-01-13 22:59

Better solution can be found in another similar question.

It's better because:

  1. Script changes name before generating sprite - not after.
  2. Because of point 1. there is no need to hardly change .css auto generated file. It's generated with correct name from beginning.
  3. Accepted solution makes cp (copy) of generated sprite with hash and it stays in file system / repo as duplicate which is quite bad. Additionally it's still seen as changed with local repo so you commit two identical files. Solution could do mv to change generated hash file name to clear one, but in this case sprite would be generated every time you use it in .scss file so it's even worse.
查看更多
登录 后发表回答