generated from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-bust.rb
51 lines (40 loc) · 1.5 KB
/
cache-bust.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# based on https://distresssignal.org/busting-css-cache-with-jekyll-md5-hash
# https://gist.github.com/BryanSchuetz/2ee8c115096d7dd98f294362f6a667db
module Jekyll
module CacheBust
class CacheDigester
require 'digest/md5'
require 'pathname'
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
local_file_name = file_name.slice((file_name.index('assets/')..-1))
File.read(local_file_name)
end
def file_contents
is_directory? ? file_content : directory_files_content
end
def is_directory?
directory.nil?
end
end
def bust_file_cache(file_name)
CacheDigester.new(file_name: file_name, directory: nil).digest!
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)