From f590810be422c62cbe9fdcf2436c3309adb41d04 Mon Sep 17 00:00:00 2001 From: Razon Yang Date: Wed, 3 Jan 2024 13:34:08 +0800 Subject: [PATCH] fix: hashing images to avoid caching issues (#1012) --- layouts/partials/helpers/image.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/layouts/partials/helpers/image.html b/layouts/partials/helpers/image.html index 934511b0..78ceddc8 100644 --- a/layouts/partials/helpers/image.html +++ b/layouts/partials/helpers/image.html @@ -22,7 +22,15 @@ {{- $static = true -}} {{- $filename := path.Join "static" $url.Path -}} {{- if fileExists $filename -}} - {{- $link = .filename | absURL }} + {{- $hash := "" }} + {{- with os.Stat $filename }} + {{- $hash = .ModTime.Format "20060102150405" | md5 }} + {{- end }} + {{- if $hash }} + {{- $link = printf "%s?v=%s" .filename $hash | absURL }} + {{- else }} + {{- $link = .filename | absURL }} + {{- end }} {{- if not (in $unresizable $ext) -}} {{- $img = imageConfig $filename -}} {{- end -}}