How to remove the hash from Compass's generated sprite image filenames?

后端 未结 4 1223
轻奢々
轻奢々 2020-12-05 08:24

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 bus

4条回答
  •  日久生厌
    2020-12-05 09:06

    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.

提交回复
热议问题