Display A Message On Old Hugo Posts

Posted on Wednesday August 25, 2021
Reading time: 2 minutes

Time moves on. Posts do not. Sometimes (read: most of the time) an old post doesn't get updated. The information in that post might be outdated and incorrect. But people don't always consider the age of the post, just jump to the content.

Taking inspiration from Twitter, I've added a message to be displayed on blog posts older than 365 days on here.

Seeing a tweet about a message on Matt Stauffer's blog for old posts, I was inspired to add the same to my own blog!

This blog is built by the static site generator Hugo, so I used the following quick-and-dirty code to display a message on posts that are more than 365 days old:

{{ $ageDays := div (sub now.Unix .Date.Unix) 86400 }}

{{ if gt $ageDays 365 }}
Warning: This post is over a year old. The information may be out of date.
{{ end }}

The following image has been taken from an old post:

A screenshot of a website with an old post on it, a banner reads Warning, this post is over a year old, the information may be out of date

Ultimately if I don't publish anything in a while, Hugo won't re-build things. As such, posts that are older than 365 days won't have the message displayed. This could be addressed by setting up a job to just re-build the site every day regardless of new posts though.

Built with Tailwind CSS using Hugo hosted on Netlify
© 2021 Matt Brunt