resources.Copy leaves a duplicate in public

Hello!

I’ve a image partial as folows:

{{- $imagePath := printf "assets/%s" .name -}}
{{- $imageOriginal := .parent.Resources.GetMatch $imagePath -}}
{{- $imageNormalized := $imageOriginal.Resize "1200x" | resources.Copy (printf "%s%s" .parent.RelPermalink $imagePath) -}}
{{- $imageExtension := index (last 1 (split (index (last 1 (split $imageNormalized.RelPermalink "/")) 0) ".")) 0 -}}
<div
  u-overflow="hidden"
  {{ range .container }}
    {{ printf "%s='%s'" .name .value | safeHTMLAttr }}
  {{ end }}>
  <img
    lazy
    alt="{{- $imageOriginal.Title -}}"
    data-src="{{- $imageNormalized.RelPermalink -}}"
    height="{{- $imageNormalized.Height -}}"
    src="{{- ($imageOriginal.Resize "64x" | resources.Copy (printf "%s%s" .parent.RelPermalink (printf "assets/%s-low.%s" (strings.TrimSuffix (printf ".%s" $imageExtension) .name) $imageExtension))).RelPermalink -}}"
    u-h="full"
    u-object="contain"
    u-transition="duration-500 ease filter"
    u-w="full"
    width="{{- $imageNormalized.Width -}}"/>
</div>

Basically, I’m generating an image with the max-width of 1200px scaled proportionally and a low-quality placeholder of width 64px. To have predictable names, I’m using resources.Copy method. However, I seem to be getting a duplicate copy in the output then:

Here, flower-1.jpg, flower-1-low.jpg, flower-2.jpg, flower-2-low.jpg is expected. The others are not. While it’s not causing breaking issues anywhere in the website, I’d like to get rid of the duplicates if possible.

Am I doing something incorrectly causing the duplicates? Is there a way to disable those?

1 Like

I think there may be a couple of things going on here…

1) When you use the .Permalink or .RelPermalink methods, the resource is published (written to the public directory). And you are using the .RelPermalink method on $imageNormalized in several places.

2) This issue.

$imageNormalized should be pointing to the “copied” version of the resource, right?

Thanks for the link to the issue. Yes, it’s happening only when resources folder exists. If I delete it, the issue disappears, that is, the images are not published anymore. Now I know why these rogue images were not there when I built the project yesterday and appeared today when I rebuilt it.

I’ll mark this thread as solved as the GitHub issue is already tracking the problem. Thanks for chiming in!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.