TIL: Debugging Hugo - fixing publish date
TL;DR Today I learned how to fix post visibility by reading the documentation https://gohugo.io/getting-started/usage/#draft-future-and-expired-content
Last night I’ve been finishing my first blog post this year. Upon finishing the text and finding the icon it’s time to fill the metadata. Midnight was minutes away so I set the publish date to 2023-01-04
(next day) and set the draft
flag to false
.
The problem
Imagine how surprising it was to notice that the article I just wrote doesn’t show up on the main page. Time to debug. First thing is to double check the draft
state - all good. Perhaps I’ve made it 2022-01-04
and the post landed a year back? nope, date’s correct.
We have to go deeper.
Investigation
In the view where my posts are listed the template code looks like this (simplified):
{{ $paginator := .Paginate (where (where .Pages "Draft" false) "Section" "posts") }}
{{ range $paginator.Pages }}
<!-- SOME HTML CODE REFERENCING POST METADATA -->
{{ end }}
Obviously it had to be something related to those where
clauses right? Let’s debug it by preparing a simple additional loop:
{{ range .Pages}}
{{ .Title}} {{ .Date }} {{ .Params.Draft }}
<hr>
{{ end}}
Awkwardly the record is not there. The next idea was to move in time, let’s change the date to some earlier date and bingo! We the post is there.
Solution
There were two issues. First - hugo ignores posts published in future (at the time of building). If I was following the documentation https://gohugo.io/getting-started/usage/#draft-future-and-expired-content more carefully it would be obvious.
The second thing was the local cache. Even though I’ve changed the date forwards and backwards, the post wouldn’t turn up. Running hugo locally with hugo serve -D --ignoreCache
has proven helpful in this case.