Jekyll is the tool I've been using to build my blog. It uses date variable to set the date of a post. This is a predefined variable, but can also be overridden in the front matter of posts.

date variable is fine if you are done with your posts once it's published. However, I find myself adding content, fixing typos and updating my posts from time to time, in which cases, I need to update the date field.

But updating date field directly may not be a good idea, because

  • the permalink of the post may get changed;
  • the info when the post was created or published is lost;
  • readers or even yourself may have no idea that the post has been updated.

So I prefer to use a separate field to record the time when the post get updated. I'm using last_modified_at variable, since it works well with Jekyll plugin jekyll-sitemap. Other names are okay too as long as appropriate variables are used in liquid templates.

Working with plugin jekyll/jekyll-sitemap

jekyll-sitemap is a plugin to silently generate a sitemaps.org compliant sitemap for you Jekyll site.

This plugin fill in the <lastmod> tag of each url with last_modified_at variable if available, otherwise just uses date variable.

Here is the related code (See on GitHub):

<url>
  <loc>{{ post.url | prepend: site_url | uri_escape }}</loc>
  {% if post.last_modified_at %}
  <lastmod>{{ post.last_modified_at | date_to_xmlschema }}</lastmod>
  {% else %}
  <lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
  {% endif %}
</url>

Show Updated field in posts

The original _layouts/post.html which is shipped with Jekyll 3.0 contains the following code:

<p class="post-meta"><time datetime="{{ page.date | date_to_xmlschema }}" itemprop="datePublished">{{ page.date | date: "%b %-d, %Y" }}</time>{% if page.author %} • <span itemprop="author" itemscope itemtype="http://schema.org/Person"><span itemprop="name">{{ page.author }}</span></span>{% endif %}</p>

Modify that part with the following to show Updated field in posts:

<p class="post-meta">
  <time datetime="{{ page.date | date_to_xmlschema }}" itemprop="datePublished">{{ page.date | date: "%b %-d, %Y" }}</time>
  {% if page.last_modified_at %}
  (Updated: <time datetime="{{ page.last_modified_at | date_to_xmlschema }}" itemprop="dateModified">{{ page.last_modified_at | date: "%b %-d, %Y" }}</time>)
  {% endif %}
  {% if page.author %}
  • <span itemprop="author" itemscope itemtype="http://schema.org/Person"><span itemprop="name">{{ page.author }}</span></span>
  {% endif %}
</p>

Show Updated field in the index page

Similarly, index.html contains following code regarding to date of posts:

<span class="post-meta">{{ post.date | date: "%b %-d, %Y" }}</span>

Replace it with the following:

<span class="post-meta">
  <time datetime="{{ post.date | date_to_xmlschema }}">{{ post.date | date: "%b %-d, %Y" }}</time>
  {% if post.last_modified_at %}
  (Updated: <time datetime="{{ post.last_modified_at | date_to_xmlschema }}">{{ post.last_modified_at | date: "%b %-d, %Y" }}</time>)
  {% endif %}
</span>

Example

Here is an example of using last_modified_at.

I created my post with the following front matter:

---
layout: post
title: 'Add an "Updated" field to Jekyll posts'
tags: [Jekyll]
date: 2016-02-13T20:47:31+08:00
---

And the date showed in the post looked like this:

Feb 13, 2016

url entry in sitemap.xml looked like this:

<url>
  <loc>https://zzz.buzz/2016/02/13/add-an-updated-field-to-your-jekyll-site/</loc>
  <lastmod>2016-02-13T20:47:31+08:00</lastmod>
</url>

Later, I found that it would be good to do some modification to my post, and updated the front matter of my post:

---
layout: post
title: 'Add an "Updated" field to Jekyll posts'
tags: [Jekyll]
date: 2016-02-13T20:47:31+08:00
last_modified_at: 2016-02-14T00:00:00+08:00
---

And got the date showed in the post looked like this:

Feb 13, 2016 (Updated: Feb 14, 2016)

url entry in sitemap.xml looked like this (using the time specified in last_modified_at instead of date):

<url>
  <loc>https://zzz.buzz/2016/02/13/add-an-updated-field-to-your-jekyll-site/</loc>
  <lastmod>2016-02-14T00:00:00+08:00</lastmod>
</url>

Done. That's it.