Storing and retrieving from global scratch pad

Hi! I’m having issues storing and retrieving information from the global Hugo scratch pad as documented here: hugo.Store. I’m not a programmer, so I don’t even how to describe what’s going wrong very well; please bear with me.

The thing that I intend to do is this: Lots of different posts on my blog end with Links sections. I would like to be able to wrap the content of each Links section in a shortcode which gathers up all the links in that section, so that they can be dumped on a Bookmarks page elsewhere.

Here’s what I’ve tried so far: I have two shortcodes gather and dump which are implemented as follows:

gather.html

{{/* gather the relevant information in the global scratchpad named $dest */}}

{{ $dest := .Get "dest" }}
{{ $exists := hugo.Store.Get $dest }}

{{ if not $exists }}
	{{ hugo.Store.Set $dest .Inner }}
{{ else }}
	{{ hugo.Store.Add $dest .Inner }}
{{ end }}

{{ .Inner | safeHTML }}

dump.html:

{{/* dump the relevant information from the global scratchpad named $source */}}

{{ $source := .Get "source" }}

{{ hugo.Store.Get $source | safeHTML }}

The issue that I’m running in to is this: When I actually try to use gather and dump, I get this weird result where dump produces many copies of the information which was gather-ed up. Moreover, it seems that the dump also has old versions of the content in global scratch pad. (See screenshot below.)

I put a minimal example on Github here:

For example, this is what I see when running the minimal example locally using hugo server.

For what it is worth, here is my hugo env output:

hugo v0.144.0-b289b17c433aa8ebf8c73ebbaf4bed973ac8e4d5 linux/amd64 BuildDate=2025-02-17T16:22:31Z VendorInfo=gohugoio
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.24.0"

Many thanks for your patience and support!

After writing this, sleeping on it, and playing around with it some more, I think the problem is this: hugo server doesn’t reset the global scratchpad between rebuilds. This means that the content of hugo.Store just keeps accumulating more and more stuff every time that page is rendered. This explains the weirdness in the screenshot.

If I build the page for “production” then I don’t have the weird reduplication.

1 Like

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