How I learned to stop worrying and love YAML front matter
When standing up this blog yesterday, I noticed that the posts on the homepage were being sorted in reverse order when deployed to my production server on Render. Let’s dive in as to why.
We read in the full list of posts only once, at startup, and store them in-memory. We hydrate a Post struct that looks like this:
type Post struct {
Title string
Slug string
Date time.Time
Content template.HTML
}
The Date field is ultimately populated by a call to fetch the ModTime() of the file:
os.Lstat(filePath)
fileInfo, err := os.Lstat(filePath)
if err != nil {
return err
}
post.Date = fileInfo.ModTime()
Here’s how the basic sort works:
sort.Slice(posts, func(i, j int) bool {
return posts[i].Date.After(posts[j].Date)
})
Pretty simple, right? So the heck what might be happening here? There are a few considerations.
My first hunch was the instinct that I’m developing on one OS and deploying to another. My local environment is running on my trusty Mac Studio, while Render deploys on Linux. Modern Linux filestystems support nanosecond-level precision while macOS provides varying levels of precision on HFS+ and APFS.
Because of this, I can’t have guarantees that the ModTime() of files created on the same day and at the same time will be different enough as to impact the sort order. In practice, this shouldn’t matter too much, but if I create multiple posts at the same time, like I did yesterday, it could.
That makes things tricky, but the real culprit is the deploy process. When deploying to Render, a fresh clone happens:
==> Cloning from https://github.com/michaelmarion/blog
==> Checking out commit cd8296ea5609a13d311201a15c5d48542999fee6 in branch main
Benny Powers has a nice write-up on a related problem. His solution—and the one recommended to me by Claude Code—is to add a more precise timestamp to the front matter of each post, or append the ISO date string to the filename. It seems like most static site generators do this as well. This will do for now.