1
0
Fork 0
mirror of https://git.sr.ht/~seirdy/seirdy.one synced 2024-12-26 18:32:10 +00:00
seirdy.one/content/notes/short-cache-busting-fingerprints-in-hugo.md
2024-03-28 01:24:25 -04:00

2 KiB

title date replyURI replyTitle replyType replyAuthor replyAuthorURI syndicatedCopies
Short cache busting fingerprints in Hugo 2024-03-28T00:16:48-04:00 https://github.com/gohugoio/hugo/issues/6241 Shorter fingerprinting (Hugo issue 6241) DiscussionForumPosting XhmikosR https://github.com/XhmikosR
title url
GitHub https://github.com/gohugoio/hugo/issues/6241#issuecomment-2024366155
title url
The Fediverse https://pleroma.envs.net/notice/AgIhXz3q628odren2G
title url
IndieNews https://news.indieweb.org/en

I use a quick crypto.FNV32a-based fix for short cache-busting fingerprints that doesn't directly rely on the unstable .Key method.

{{}}{{}}

I use Hugo's crypto.FNV32a to generate a short hash, then copy the resource to a new path with that fingerprint.

{{}}

{{ $resource := resources.Get . -}}
{{- $target_path_formatStr := (replaceRE `(\.[^\.]*)$` ".%d$1" .) -}}
{{- $cacheBuster := $resource.Content | crypto.FNV32a -}}
{{- $target_path := printf $target_path_formatStr $cacheBuster -}}
{{- return resources.Copy $target_path $resource -}}

{{}}

{{}}{{}}

You can see it used in my site's head element. I invoke it using partialCached so the fingerprinting only happens once per resource:

{{}}

{{ $icon_svg := partialCached "cache-bust.html" "/favicon.svg" "/favicon.svg" }}
{{- printf `<link rel="icon" sizes="any" href="%s" type="image/svg+xml" />` $icon_svg.RelPermalink | safeHTML }}

{{}}

{{}}{{}}

Here's a snippet of the final rendered result:

{{}}

<link rel="icon" sizes="any" href="/favicon.2229316949.svg" type="image/svg+xml"/>

{{}}

Encoding it to a higher base and using alphanumerics could shave off 1-2 ch.