HowTo — Thinking 🤔 2022.12.27 2022.12.29

Getting Last Modified Working in Hugo

A how-to guide in how to get last modified to show up nicely

Which, as it stands, isn’t a hard problem. I still managed to get it tangled up a bit. It helps having your config separated into dev and prod, though. This is also made with the presumption that you store your site’s documents in git.1

What I wanted to do here, was to display the last modified date on my posts. This was for two reasons.

  1. These are thoughts that are refined over time, and it helps me — and you — to see what has been changed recently. On the site and, in time, in RSS.
  2. I forgot the second reason. TBD.

How to make last modified date be updated

Again, not hard. I just needed to not set lastmod in each posts frontmatter.2 That’s it. If it is defined, it wants use it. Well, the date defaults per the Hugo docs says first git, then the hardcoded lastmod, then the hardcoded date, then finally the hardcoded publishDate.

How to make the website get updated from git

It’s also metioned in the same docs, just scroll a bit down (to the :git bit). Just find your config/production/config.toml file, and put enableGitInfo = true at the top, just around the baseURL = '' bit (which I totally needed to get CSS working locally).

This should get the lastmod bit updated, as long as you remember to commit to git before generating the site with the hugo command. Not a problem if you write locally and push to get updated, but still.

How to make the local server update before git commit

Because you might want to see that your site behaves predictable, perhaps with last updated bubbling to the top of lists or something.

This was also handled in config, and — surprise! — it is mentioned in that same hugo documentation. Just above the :git bit. It says :fileModTime, and the example to put in your config/development/config.toml file is:

[frontmatter]
  lastmod = ['lastmod', ':fileModTime', ':default']

This will look first in your post’s frontmatter (metadata), and then at the date/time your file was last saved. Which should serve nicely.

That bit should be it’s own block of config, much in the same way as [params].

Bonus for those who read all the way here

If you have some code to list blog posts or something, it will show several using this type of code to show, for example, 6 posts in the blog section:

{{ range ( where site.RegularPages "Section" "blog" | first 6 ) }}

In order to list recently updated, if you would want that, just add a bit in the middle:

{{ range ( where site.RegularPages.ByLastmod.Reverse "Section" "blog" | first 6 ) }}

This might not be much of a bonus, but I probably spent more time writing it than you reading it. So no complaining. ;-)


  1. Using git? Post to come later, maybe? ↩︎

  2. Frontmatter: The bits between the dashes or plusses at the top of each post. Where the title is. ↩︎