Skip to content

Instantly share code, notes, and snippets.

@daneden
Created October 17, 2013 15:43
Show Gist options
  • Save daneden/7027258 to your computer and use it in GitHub Desktop.
Save daneden/7027258 to your computer and use it in GitHub Desktop.
I whipped up a cache-buster for Jekyll, similar to the jekyll-assets method. Basically, when you run `jekyll build`, cachebuster.rb generates a hash based on the current time, and shoves it into the default.html template. Requests for style-{hash}.css are redirected to style.css by .htaccess
RewriteRule (.+)-[a-zA-Z0-9]+(\.(js|css))$ $1$2 [L]
# /_plugins/cachebuster.rb
require 'digest/sha1'
module Jekyll
class CacheBuster < Liquid::Tag
def render(context)
"#{Digest::SHA1.hexdigest(Time.now.to_s)}"
end
end
end
Liquid::Template.register_tag('bust_cache', Jekyll::CacheBuster)
<!-- /_layouts/default.html -->
<html lang="en">
<head>
<!-- ... -->
<link rel="stylesheet" href="/assets/css/style-{% bust_cache %}.css">
<!-- E.G. Outputs <link rel="stylesheet" href="/assets/css/style-afdebe725f30fd53978fabf045308bd1127b3a95.css"> -->
<script src="/assets/js/app-{% bust_cache %}.js"></script>
<!-- ... -->
</head>
<body>
<!-- ... -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment