I was about to ask this question and as I was writing it out, I thought I should have one last look in hugo docs, and lo and behold, I got a solution.
Basically, for cachebusting I wanted to append git commit hash of css file
<link rel="stylesheet" href="style.css?v=66379f8">
In my deploy script written in bash, I have exported said hash.
git
has a very handy way of getting last commit hash of individual file or directory.
export CSS_HASH="$(git log -1 --format='%h' path/to/css/)"
In hugo 1.4, there is a new function getenv
. According to docs, it returns empty string if variable not set, else it returns said environment variable.
So, while doing development, if CSS_HASH is not exported, the css url will have ?v=
empty param. If you are like me and it bugs you, this can be fixed by -
<link rel="stylesheet" href="style.css{{with getenv "CSS_HASH"}}?v={{.}}{{end}}"
This will give nice url both in development and production.
On the sidenote, If anyone can tell about doing if...else
with getenv
variables.