# Persisting data across the build of a site

**URL:** <https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114>\
**Category:** support\
**Created:** [October 26, 2022, 11:30pm UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114 "2022-10-26T23:30:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bchmura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bchmura/32/16273_2.png) [@bchmura](https://discourse.gohugo.io/u/bchmura)\
**Post date:** [October 26, 2022, 11:30pm UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114/1 "2022-10-26T23:30:29Z")

</div>

Hi,  
I am trying to implement a random image to break up my wall-of-text posts.

I have a pile of images in an assets directory and am able to pull the list of them into a short code and randomly pull one out. **The only issue is that this can result in duplicates being randomly selected during the build** even on the same page.

Since these are in the markdown content, I believe I have to use a shortcode.

Is there a way to have a “global variable” that I can use to store the image list, modifying it each time I use an image so that is not an option for future calls to the shortcode?

Thank you, as always, for any help!

---

<div class="post-metadata">

**Author:** ![fekete-robert](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fekete-robert/32/13363_2.png) [@fekete-robert](https://discourse.gohugo.io/u/fekete-robert)\
**Post date:** [October 27, 2022, 11:36am UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114/2 "2022-10-27T11:36:47Z")

</div>

Maybe using Scratch? [.Scratch | Hugo](https://gohugo.io/functions/scratch/#readout)

---

<div class="post-metadata">

**Author:** ![bchmura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bchmura/32/16273_2.png) [@bchmura](https://discourse.gohugo.io/u/bchmura)\
**Post date:** [October 28, 2022, 12:05am UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114/3 "2022-10-28T00:05:46Z")

</div>

Thanks, I could be wrong… I was looking at that, but it seemed like both were either tied to the page context or just a local scope for newscratch.

It would help stop duplicates on the same page which would be a huge help regardless of being reused on the rest of the site

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 28, 2022, 9:25pm UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114/4 "2022-10-28T21:25:43Z")

</div>

You can attach a Scratch to another page. For example, to set a Scratch on the home page:

```auto
{{ site.Home.Scratch.Set "foo" "bar" }} 

```

So your shortcode would look something like:

```auto
{{- $i := "" }}

{{- range $r := resources.Match "random-images/*" | shuffle }}
  {{- with site.Home.Scratch.Get "imagesUsed" }}
    {{- if in . $r }}
      {{- continue }}
    {{- else }}
      {{- $i = $r }}
      {{- site.Home.Scratch.Add "imagesUsed" $i }}
      {{- break }}
    {{- end }}
  {{- else }}
    {{- $i = $r }}
    {{- site.Home.Scratch.Set "imagesUsed" (slice $i) }}
    {{- break }}
  {{- end }}
{{- end }}

{{- with $i }}
  {{- with .Resize "200x" -}}
    <img src="{{- .RelPermalink }}" width="{{- .Width }}" height="{{- .Height }}" alt="{{- .Name | path.BaseName | humanize }}">
  {{- end }}
{{- else }}
  {{- warnf "There are not enough random images to choose from. See %s" .Position }}
{{- end -}}

```

However, because Hugo builds pages concurrently, by default this will _not_ yield a unique image for each instance of the shortcode. You can get around this by setting the number of threads to 1.

```bash
HUGO_NUMWORKERMULTIPLIER=1 hugo server

```

There may be another way around the concurrency issue, but it’s not coming to me at the moment.

---

<div class="post-metadata">

**Author:** ![bchmura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bchmura/32/16273_2.png) [@bchmura](https://discourse.gohugo.io/u/bchmura)\
**Post date:** [October 29, 2022, 8:30pm UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114/5 "2022-10-29T20:30:55Z")

</div>

Whoa… I did not realize that an object (or whatever construct it has) would exist for other pages even when not building them… I guess in retrospect it needs to.

That is a really cool solution to my challenge! Thank you very much

Bill

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 30, 2022, 5:27pm UTC](https://discourse.gohugo.io/t/persisting-data-across-the-build-of-a-site/41114/6 "2022-10-30T17:27:19Z")

</div>

> [@jmooring](#):
>
> There may be another way around the concurrency issue

There is.

In your baseof.html template, at the top:

```auto
{{- partialCached "init.html" . -}}

```

layouts/partials/init.html

```plaintext
{{- range site.AllPages }}
  {{- $noop := .Content }}
{{- end -}}

```

What does this do? When the first page (any page) is rendered, the `.Content` of every page is rendered, sequentially, and assigned to a throw-away variable (`$noop`).

Here’s a working example:

```bash
git clone --single-branch -b hugo-forum-topic-41114 https://github.com/jmooring/hugo-testing hugo-forum-topic-41114
cd hugo-forum-topic-41114
hugo server

```
