/etc

Originally Published: 2020-04-27

If you’re hosting a blog on GitHub Pages, sometimes you wish you could include a “last modified” snippet on posts which you’ve revisited, updated, or modified after the initial publication. Sure, there’s a jekyll-last-modified-at plugin, but it’s not part of the GitHub Pages plugin whitelist, so you would have to locally build your site into HTML on each commit for GitHub Pages to statically serve pages with a baked-in “last modified” time. You could also include e.g. a last_modified variable in your YAML Front Matter which you manually update, but if you’re trying to add it to a site which has a lot of old posts, this might be somewhat error-prone and tedious.

For this blog, I decided to add a small bit of JavaScript to my default layout which I call on page load, which uses the GitHub API to fetch the latest commit for the current page, compare it to the original publication date, and add it to the page if they differ:

(Full code here)

This uses the jekyll-github-metadata plugin to get the correct owner and repository. I also use Polyfill.io to add support for fetch to older browsers.

Note that it’s important to put this JavaScript somewhere where page.path will be expanded correctly, or alternately you could pass it in as a JS variable if you want to get fancy.