Interesting idea. The only thing I can think of off the top of my head is, use an alias in the latest markdown. That’s manual and imo inelegant, though.
I wouldn’t try to solve this with Hugo, this looks like a dynamic that you could handle with Javascript.
Take a look here:
He uses a Json file to create a dynamic redirect in the 404 page. The same could be done with hugo, listing everything in a section and directing the user to the latest page in that section.
In this case it does not work like a redirect right?
This creates a new page with url "https://example.com/posts/latest " that uses the content form the newest page of the posts section?
This is a pretty interesting approach, and might for my case be even better than a redirect.
But this will mean that when i range over all the pages of the “posts” section, this extra page will also show up, since the “latest” section is nested under the “posts” section.
Created /layouts/_default/latest.html based on my single.html because it had the right headers and footers already applied, then replaced the relevant code in the main block. See below.
Created /content/latest/_index.md with some basic frontmatter filled.
My latest.html layout defines the main block like this between header and footer sections:
The .Title on line 4 is that of the _index.md file. The other .Title inside the range loop is from the result page of the range. I like the focus of .Site.RegularPages better than the all-encompassing .Site.Pages so I used that.
Additionally, if I was really trying to put this into production, I would be concerned that, the section page’s date is coming from the _index.md which has to be manually updated, or scripted. One solution candidate is to use a git hook to run a script to increment the date, either lazily using today’s date, or, somehow copy-pasting the latest post’s date to the section page frontmatter.
Another point is, I’ve heard search engines penalize sites for having duplicate content. As a static page, this is truly a static and duplicate page. It kind of nudges me in the direction of javascript for this particular thing…