--minify change the value of .Scratch.Get "key"

I came here after a redirection from github !

$ hugo version v0.88.1

I have a different result with --minify and without

I changed a template to create a page that uses variables of the FrontMatter.

An example is:

title: "Test TITLE"
date: 2021-10-07
authors: "**Lecomte, H.**"
abstract: "abstract test"
publication: "pub test"

This content is read by my file ‘themes/perso/layouts/_default/single.html’, here is the part of the file concern. It activates only if the key “abstract” is set.

{{ if isset .Params "abstract"}}
      <h4>by {{ $.Scratch.Get "authors" | markdownify}}<br>
       <i>{{ $.Scratch.Get "publication" }}</i><br>

      {{ $.Scratch.Get "date" }}</h4>
{{ end }}

The command

$ hugo

give me the following attended output in the index.html webpage

<h4>by <strong>Lecomte, H.</strong><br>
          <i>pub test</i><br>

      October, 2021</h4>

But then with

$ hugo --minify

I got a result without any value…

<h4>by <br>

This change of behaviour seems strange. Maybe I don’t have the best practice with the {{$.Scratch.Get “key” }} but it works in one case so I don’t understand.
The comportment of this bug is strange because in some random run it doesn’t reproduce. (but it mostly bug ^^)

Thanks in advance for reading this!

Assuming this is your project’s repository…

First, the presence of a static file (an image) in the root of your content directory is triggering this bug. Fix this with:

mv content/hugo_lecomte_fieldwork.jpg static/

Second, the intermittent loss of data is not related to minification. Run this command repeatedly and you will occasionally see the problem:

rm -rf public/ && hugo --buildFuture && grep -A5 "<h4>" public/publications/test2020test/index.html 

Third, you can reproduce the problem every run by limiting the number of CPUs used during processing:

rm -rf public/ && HUGO_NUMWORKERMULTIPLIER=1 hugo --buildFuture && grep -A5 "<h4>" public/publications/test2020test/index.html 

This is clearly an issue with your theme, not a bug.


Prior to v0.48.0 (August 29, 2018) you could not assign a new value to a variable once it had been initialized. This was due to a Golang limitation that took over three years to address.

The Hugo “Scratch” was created to work around this limitation. Unfortunately, this outdated construct still exists in examples, tutorials, and themes.

There are valid reasons to use a Scratch, but this isn’t one of them.

1 Like

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