Repository: gohugoio/hugo (language: go)

Issue:
RSS template has incorrect value for lastBuildDate
### What version of Hugo are you using (`hugo version`)?

<pre>
$ hugo version
hugo v0.119.0-b84644c008e0dc2c4b67bd69cccf87a41a03937e+extended darwin/arm64 BuildDate=2023-09-24T15:20:17Z VendorInfo=brew
</pre>

### Does this issue reproduce with the latest release?

Yes

### Steps to reproduce

1. Build site with `hugo` command
2. Check the top-level `/index.xml` file

### Description

In Hugo 0.119, the [internal RSS template](https://github.com/tastapod/hugo/blob/272484f8bfab97dbadad49a638a3e4b6af499f15/tpl/tplimpl/embedded/templates/_default/rss.xml#L46) has a `<lastBuildDate>` field based on `.Date`, which is the date in the site's top-level `_index.md` file (or the `_index.md` file for the section or category for sub-feeds, I presume).

Instead it should either be the date the RSS file was built or the date of the most recent published post, so RSS readers can tell whether anything has changed.

I am doing the latter in the following fragment, in a custom `/layouts/rss.xml` template based on the internal one:

```go
{{- $lastBuildDate := (index $pages.ByDate.Reverse 1).Date -}}
...
<lastBuildDate>{{ $lastBuildDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
```

I am happy to submit a PR with this change. I can't see it breaking anything except for the case where `$pages` is empty.

Using the current build time is even easier:

```go
<lastBuildDate>{{ now.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
```

I prefer the most recent post date; it feels more RSS-like. The main thing is that the current `/index.xml` bases the date on `/_index.md`, which in my case is August 2013!


Discussion / hints:
Yes, we should change this, but use `.Lastmod` instead of `.Date` and remember that array/slice indexes are zero-based:

```
<lastBuildDate>{{ (index $pages.ByLastmod.Reverse 0).Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>
```

Please submit PR.
Ah thanks, I misread the collections.Index docs. Will do.
I found another [RSS template][rss], which is for the Hugo docs site. This has the same bug.

I propose factoring out the expression in [line 20][line20] so the first line of the template is:

```go
{{- $pages := first 50 (where .Site.RegularPages "Type" "in" (slice "news" "showcase")) -}}
```

then use the same `(index $pages.ByLastmod.Reverse 0).Lastmod` expression for the `lastModDate` field.

WDYT?

[rss]: /gohugoio/hugo/blob/master/docs/layouts/index.rss.xml
[line20]: /gohugoio/hugo/blob/705e3cd5f558fb506f25c4255968bfdc907adb29/docs/layouts/index.rss.xml#L20
I think you should focus on the original issue, and raise a separate issue in the docs repository.
Ok will do.

I'm getting test failures on a fresh checkout. I've [asked for help][discord] but nothing yet. I'm pretty sure this change won't make things any worse, but I have an allergic reaction to checking in code with broken tests!

[discord]: https://discourse.gohugo.io/t/mage-tests-timing-out-on-fresh-checkout/46809

Fix patch:
diff --git a/tpl/tplimpl/embedded/templates/_default/rss.xml b/tpl/tplimpl/embedded/templates/_default/rss.xml
index a4cdd2383a2..6561fb3218f 100644
--- a/tpl/tplimpl/embedded/templates/_default/rss.xml
+++ b/tpl/tplimpl/embedded/templates/_default/rss.xml
@@ -53,7 +53,7 @@
     <managingEditor>{{.}}{{ with $authorName }} ({{ . }}){{ end }}</managingEditor>{{ end }}{{ with $authorEmail }}
     <webMaster>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</webMaster>{{ end }}{{ with .Site.Copyright }}
     <copyright>{{ . }}</copyright>{{ end }}{{ if not .Date.IsZero }}
-    <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
+    <lastBuildDate>{{ (index $pages.ByLastmod.Reverse 0).Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
     {{- with .OutputFormats.Get "RSS" }}
     {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
     {{- end }}


Test patch: