Skip to content

Instantly share code, notes, and snippets.

@yaroslav
Last active July 7, 2023 12:43
Show Gist options
  • Save yaroslav/a1acc36b49820474b4e0218c0ca8908d to your computer and use it in GitHub Desktop.
Save yaroslav/a1acc36b49820474b4e0218c0ca8908d to your computer and use it in GitHub Desktop.
Jekyll assets cachebuster (aka fingerprint) filter — no asset pipeline, no nonsense, just a single simple filter. Must-have for CDN-enhanced Jekyll sites.
require 'digest'
module Jekyll
# Jekyll assets cachebuster filter
#
# Place this file into `_plugins`.
module CachebusterFilter
# Usage example:
#
# {{ "/style.css" | cachebuster }}
# {{ "/style.css" | cachebuster | absolute_url }}
def cachebuster(filename)
sha256 = Digest::SHA256.file(
File.join(@context.registers[:site].dest, filename)
)
"#{filename}?#{sha256.hexdigest[0, 6]}"
rescue
# Return filename unmodified if file was not found
filename
end
end
end
Liquid::Template.register_filter(Jekyll::CachebusterFilter)
@patrickfav
Copy link

This doesn't seem to work with jekyll 3.8; it creates the hashes, but file changes wont create different hashes

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