Last active
July 7, 2023 12:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to work with jekyll 3.8; it creates the hashes, but file changes wont create different hashes