I use partial below to create responsive images. Everything works fine when working on in-memory server, things breaks when generating deployment.
To make sure no stale cache interfere with the result I use hugo --minify --ignoreCache --cleanDestinationDir --gc
to generate deployment. After that in public folder I get image file as expected but half of them are broken, and can’t be open in any image viewer. All images I use are JPG.
The partial I use for every image:
{{ $placeholder := "images/placeholder.jpg" }}
{{ $params := (dict "lazy" true "sizes" (dict) "class" "" "alt" "" "title" "") }}
{{ $params = merge $params . }}
{{ $width := "" }}
{{ $height := "" }}
{{ $sizes := ""}}
{{ if $params.size }}
{{ $size := split .size "x" }}
{{ $width = index $size 0 }}
{{ $height = index $size 1 }}
{{ end }}
{{ $srcset := "" }}
{{ $src := "" }}
{{ if $params.static }}
{{ $src = $params.src | relURL }}
{{ else }}
{{ with $params.page.Resources.GetMatch $params.src }}
{{ $orig := . }}
{{ if eq $width "" }}
{{ $width = $orig.Width }}
{{ end }}
{{ if eq $height "" }}
{{ $height = $orig.Height }}
{{ end }}
{{ if $params.size }}
{{ $src = ($orig.Resize $params.size).RelPermalink }}
{{ else }}
{{ $src = $orig.RelPermalink }}
{{ end }}
{{ $index := 0 }}
{{ range $key, $value := $params.sizes }}
{{ $w := index (split $value "x") 0 }}
{{ if eq $index 0 }}
{{ $srcset = printf "%s %sw" ($orig.Resize $value).RelPermalink $w }}
{{ $sizes = printf "%s %svw" (printf "(min-width: %spx)" $key) $w }}
{{ else }}
{{ $srcset = printf "%s, %s %sw" $srcset ($orig.Resize $value).RelPermalink $w }}
{{ $sizes = printf "%s, %s %svw" $sizes (printf "(min-width: %spx)" $key) $w }}
{{ end }}
{{ $index = add $index 1 }}
{{ end }}
{{ end }}
{{ end }}
{{ with $params }}
<img src="{{ if .lazy }}{{ $placeholder | relURL }}{{ else }}{{ $src }}{{ end }}" alt="{{ .alt }}" class="{{ if .lazy }}lazyload{{ end }} {{ .class }}" data-src="{{ $src }}" srcset="{{ $srcset }}" sizes="{{ $sizes }}" title="{{ with $params.title }}{{ . }}{{ end }}">
<noscript>
<img src="{{ $src }}" alt="{{ .alt }}" class="{{ .class }}" srcset="{{ $srcset }}">
</noscript>
{{ end }}
Example invocation looks like this
{{ partial "image" (dict "src" (printf "images/%s" .Params.Image) "alt" (printf "Thumbnail for '%s' post" .Params.Title) "size" "260x146" "sizes" (dict "320" "206x116" "375" "260x146" "425" "315x177" "768" "365x205" "1024" "220x124") "page" .) }}
As we know dict is unordered data type so maybe there is a problem with cache when we resize multiple times the same image with different parameters, bigger and smaller at random order.
I also tried replacing Resize
with Fit
, results are similar - half of images are broken.
I use macOS 10.15.7, Hugo v0.79.0-DEV/extended.