Following my recent SEO issues , I’m actively exploring ways to bolster my website’s ranking. One aspect I’m keen to address is expediting Search Engine indexing for pages that have been altered or updated. This is precisely the purpose of Sitemaps, a feature fully supported by Hugo1. However, there’s a limitation in how Hugo handles this by default. It sets the lastmod parameter to either the page’s creation time or the last build time. What I aim to achieve is a clear separation between creation and modification dates. This would signal to Search Engines to focus on scrutinizing only the “changed” posts, expediting their reevaluation—an approach commonplace in the Wordpress realm but more intricate with Hugo.

Fortunately, I stumbled upon the lastmod field that can be added to a post’s front matter. While this allows manual updating on a per-post basis, relying on memory might be fallible. Given that my pages are stored in a Git repository, I sought a solution to automate this process with some kind of pre-commit or post commit hook, but I discovered that Hugo offers just that 🤓 2

To enable this feature, simply add the following line to your hugo.yaml configuration:

hugo.yaml
enableGitInfo: true

However, there’s a caveat: this feature requires full Git history; shallow clones won’t suffice.

For those employing Github Actions for their build pipeline, adjusting the checkout action is necessary. Set fetch-depth to 0 to ensure the full repository history is fetched:

Github Actions unshallow
- uses: actions/checkout@v4
  with:
    submodules: true
    fetch-depth: 0

As for myself, having recently migrated to Cloudflare Pages 3, I had to tweak the build command accordingly:

Cloudflare Pages build command
git fetch --unshallow && hugo --minify

This same approach can be applied to other CI/CD configurations seamlessly.


Enjoyed? Buy Me a Coffee at ko-fi.com