Skip to content

Instantly share code, notes, and snippets.

@jonsage
Last active February 5, 2021 12:54
Show Gist options
  • Save jonsage/82bea8bc62389b3c45485fe5b12a6534 to your computer and use it in GitHub Desktop.
Save jonsage/82bea8bc62389b3c45485fe5b12a6534 to your computer and use it in GitHub Desktop.
Registers :build permalink placeholder that is replaced with the site build time (useful for cache-busting files)
# https://articles.inqk.net/2020/02/10/jekyll-permalinks.html
# place in `_plugins` directory
module Jekyll
module Drops
class UrlDrop < Drop
def build
@context.registers[:site].time.strftime("%s")
end
end
end
end
@pyrmont
Copy link

pyrmont commented Feb 5, 2021

What's your configuration file look like?

@pyrmont
Copy link

pyrmont commented Feb 5, 2021

Also, is @context available in a Drop? I used @obj because it should represent the site object document object (for which the permalink is being generated).

I can't remember anymore what data you can access from that object; I assume if you need the site object you can get to it but not sure how off the top of my head.

@pyrmont
Copy link

pyrmont commented Feb 5, 2021

Hmmm, have had a look around the Jekyll codebase and I think there should be a .site method accessible on @obj that will get you the site. I don't know if that object has a .time method on it but maybe give that a try and see how it goes.

To be clear, rather than:

@context.registers[:site].time.strftime("%s")

you'd have:

@obj.site.time.strftime("%s")

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