.Scratch in partial weird in Netlify

Hi everyone,

I’m using Hugo 0.134.0, Go 1.23.1 and Hugo Cache Resources. My build command is hugo --noChmod --gc --minify.

In Netlify, when I repeat build several times, something weird happens.

Often .Scratch.Get "large" works but sometimes not, it doesn’t take true in the item partial.
If I clear cache and rebuild in Netlify, it displays correctly.

Example code:

items.html

{{ range $pages }}
  {{ .Scratch.Set "large" true }}
  <div class="{{ if .Scratch.Get "large" }}is-large{{ end }}>
    {{ partial "item" . }}
  </div>
{{ end }}

item.html

<div class="{{ if .Scratch.Get "large" }}is-large{{ end }}>
    {{ .Title }}
</div>
{{ .Scratch.Delete "large" }}

I need to keep the context in {{ . }}
Could you please help me?
I don’t know if I’m using .Scratch wrong or not.

Thanks a lot
Seb

Hust a guess:

you are facing this one due to the parallel building of Hugo:
why-is-my-page-scratch-or-store-missing-a-value

  • you range over a set of pages
  • assign a value to each pages scratch
  • pass the page to next partial
  • at this time the page may not be rendered and so there’s no access to the Scatch Value…

btw: your class= code is broken (non closed string)

you could pass the "large to the partial

{{ partial "item" (slice . true) }}
{{ $large := index . 1 }}
{{ with index . 0 }}
{{/* now here the "." is the page *//
  <div ... {{ if $large ... }}

Why do you need to use a scratch here? Can’t you just pass the value in a map to the partial? For example:

{{ $m := dict "page" . "foo" true }}
{{ partial "something.html" $m }}

Because in something.html, I’m using context like {{ . }} not like {{ .page }}.

something.html can be use in another context like {{ partial "something.html" . }}

Thanks @irkode for this explains.
I don’t really understand the usefulness of .Scratch if it’s not reliable to pass it in a partial.

So the only way to pass additional data to a .Page is to pass it as an argument to the partial to make it robust.

Thanks a lot

The Scratch and Store methods are quite reliable. I always use the latter because it survives rebuilds when running hugo server.

As @irkode describes, the most common mistake made by site and theme authors is that they ignore concurrency, attempting to “get” a scratch pad value before it has been set. This condition, and the proper way to handle it, is described here:

https://gohugo.io/troubleshooting/faq/#why-is-my-page-scratch-or-store-missing-a-value

So using {{ $noop := .Content }} before doing a .Scratch.Get ensures that you have the value available.

OK thanks @jmooring and @irkode.

Hi,

does {{ $noop := .Content }} work if I have no content? I don’t think so.

Thanks

Mmh. Pages are rendered by templates. So you will at least get the page frame with an empty body.

So it should work for a minimal .md file with empty frontmatter

---
---

Any specific problem encountered. Maybe I missed something.