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

后端 未结 4 1196
轻奢々
轻奢々 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 08:40

    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.
    0 讨论(0)
  • 2020-12-05 08:47

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

    0 讨论(0)
  • 2020-12-05 08:59

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题