Add date updated or "last modified" timestamp to feed.json

Is there a way to customize the feed.json template so that it includes the updated date of the post?

Currently, it only includes “date_published”.

If you’re comfortable editing layouts/index.json in your theme, you could add it there. date_modified appears to be the correct key name based on the spec.

1 Like

right, thank you, found the template!

Looks like there are many ways to get “last modified” from hugo, including from the file’s modified date or from git, or from a date you add to frontmatter. I tried several options but nothing seems to be able to be picked up.

Tried this in config.json:

    "title": "[TITLE]",
    "enableGitInfo": "true",
    "frontmatter": {
        "lastmod": [

Tried this in index.json:

"content_html": {{ $s }},
"date_published": "{{ .Date.Format "2006-01-02T15:04:05-07:00" }}",
"date_modified": "{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" }}",
"url": "{{ .Permalink }}"

@manton is there a param that can be picked up for this? I don’t know your build process to know what should work…

Sorry, it looks like we’re not actually adding the last modified date to the front matter. I’ll add that to so that .Lastmod is available. Not sure how it was overlooked, I guess because none of the basic themes use it.

Should roll out tomorrow.

1 Like

oh, thanks a bunch! I know there’s one plugin that adds the meta tags for twitter etc, that assumes its presence, so this will probably have a good positive impact.

I’ll test and report back, once it’s in.

Question, should I need anything in config.json to get this working? Or should it “just work”.

It should just work. I don’t think anything special in config.json is needed.

1 Like

This is live. For a post that has never been modified, the .Lastmod will be the same as .Date. This seems like the right thing to do. We could also make it empty, but I worry that puts more effort on theme developers to check the value.

1 Like

Thanks! A cursory check shows it working in my feed. Iirc Hugo defaults lastmod to date created for new posts, and I agree that’s how it should be. Exception checking is a pain. :+1:

hi @manton, I noticed that the feed.json is filtered to show only posts, but, I’m also interested in finding out whether pages are updated. I looked at the source of a couple of my pages, and it appears that the last modified date is not present.

Would it be possible to have .Lastmod work on pages, as well?

1 Like

{{- $list := first 25 (where .Site.Pages "Type" "post") -}}

Is in index.json.

Feel free to just take out the where clause and just use .Site.Pages.

1 Like

Oh, awesome! I had tried to show last modified on my posts before, but figured I was doing something wrong. It was a nice surprise to see the code I left in randomly start working :)

{{ $date := .Date.Format "January 2 · 15:04" }}
{{ $lastmod := .Lastmod.Format "January 2 · 15:04" }}
<time pubdate itemprop="datePublished" datetime="{{ .Date.Format "2006-01-02 15:04:05 -0700" }}">
    {{ $date }}
    {{ if ne $lastmod $date }}
        (Updated: {{ $lastmod }})
    {{ end }}