Skip to content

Instantly share code, notes, and snippets.

@BryanSchuetz
Created February 17, 2018 16:19
Show Gist options
  • Save BryanSchuetz/2ee8c115096d7dd98f294362f6a667db to your computer and use it in GitHub Desktop.
Save BryanSchuetz/2ee8c115096d7dd98f294362f6a667db to your computer and use it in GitHub Desktop.
CSS Cache Bust Jekyll Plugin
module Jekyll
module CacheBust
class CacheDigester
require 'digest/md5'
attr_accessor :file_name, :directory
def initialize(file_name:, directory: nil)
self.file_name = file_name
self.directory = directory
end
def digest!
[file_name, '?', Digest::MD5.hexdigest(file_contents)].join
end
private
def directory_files_content
target_path = File.join(directory, '**', '*')
Dir[target_path].map{|f| File.read(f) unless File.directory?(f) }.join
end
def file_content
FIle.read(file_name)
end
def file_contents
is_directory? ? file_content : directory_files_content
end
def is_directory?
directory.nil?
end
end
def bust_css_cache(file_name)
CacheDigester.new(file_name: file_name, directory: 'assets/_sass').digest!
end
end
end
Liquid::Template.register_filter(Jekyll::CacheBust)
@KenHV
Copy link

KenHV commented Jul 7, 2024

Make sure to change this line for cache busting to work:

-         [file_name, '?', Digest::MD5.hexdigest(file_contents)].join
+         [file_name, '?v=', Digest::MD5.hexdigest(file_contents)].join

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment