.Store keeps values over server reloads

Something that worked until recently is this “system” I used:

  • an SVG icon shortcode {{< icon name="bla" >}}
  • if the specific icon is used the first time the shortcode echoes a <symbol> tag with the icon and a <use> tag with a reference to that symbol
  • if the specific icon is used a second time then the shortcode only echoes the <use> tag - some kB saved.

This worked fine with scratch, until that got deprecated. When I just looked over it to adapt to the latest code, I realised, that .Store apparently stays saved over server reloads. So when I change something in the template of a page, the icons disappear, because the first (for this server reload) call to the icons finds a note in the store that it was already echoed.

After a hugo server starts the icons are displayed. Once I change something, they disappear. Running the build of course works fine. The same happens everywhere I did have scratches.

Is there any way to somehow define a scratch page wide like “in the olden days” that is created new when the page/server is reloaded? Maybe I am just missing something fundamental here.

The layout in question (I tried with page, hugo, and site for the .Store but all lead to the same behaviour):

icon.html
...
{{- $icons := page.Store.Get "icons" -}}
<svg width="{{- $width -}}" height="{{- $height -}}" class="icon icon-{{- $iconname }} {{ $class -}}">
  {{ if $icons}}
    {{- if compare.Eq (collections.Index $icons $iconname) true -}}
      {{- /* symbol already included */ -}}
    {{- else -}}
      {{- /* include symbol definition */ -}}
      {{ partial "inline/dnb/icons/lucide/get-icon.html" (dict "iconname" $iconname) }}
    {{- end -}}
  {{ else }}
    {{- /* first call to the shortcode, include symbol definition */ -}}
    {{ partial "inline/dnb/icons/lucide/get-icon.html" (dict "iconname" $iconname) }}
  {{ end }}
  <use href="#{{- $iconname -}}"{{ with .width }} width="{{- . -}}"{{ end }}{{ with .height }} height="{{- . -}}"{{ end }} />
</svg>

{{ define "_partials/inline/dnb/icons/lucide/get-icon.html" }}
  {{ $iconname := .iconname }}
  {{ $iconpath := fmt.Printf "icons/lucide/%s.svg" $iconname }}
  {{ $content := "" }}
  {{ with resources.Get $iconpath }}
    {{ $content = .Content | safe.HTML }}
    {{ $content = replaceRE `<svg[\t\r\n ]+` `<svg ` $content }}
    {{ $replaceId := fmt.Printf "id=%q" $iconname | safe.HTMLAttr }}
    {{ $replacement := fmt.Printf "<symbol %s " $replaceId }}
    {{ $content = strings.Replace $content "<svg " $replacement 1 }}
    {{ $content = strings.Replace $content "</svg>" "</symbol>" 1 }}
    {{ page.Store.SetInMap "icons" $iconname true }}
  {{ end }}
  {{ $content | safe.HTML }}
{{ end }}

created a one pager, where I added the shortcodes: (v0.154.2)

really? I can change the index.md, home and base template … and get the display.

only when I’m changing the shortcode or your inline partial it breaks.

HINT: following the warning for global page function I would use the .Page method within shortcode and pass it to the inline.

{{ $page := .Page }}
{{- $icons := $page.Store.Get "icons" -}}
...
{{ partial "inline/dnb/icons/lucide/get-icon.html" (dict "iconname" $iconname "page" $page ) }}

...
{{- define "_partials/inline/dnb/icons/lucide/get-icon.html" -}}
...
{{ .page.Store.SetInMap "icons" $iconname true }}
1 Like

I have a video embed that adds some javascript on the first load in the same way and it disappears every time I edit “something somewhere”. Interestingly both layouts with the store live in modules. I’ll play around on what changes each stops working. Maybe I need to “watch” module changes with a cachebuster?

I think in this context it’s ok to take the top-level page object. I want to output each individual icon once per pageload, not once per page item (content item), I think. I’ll test that tomorrow morning with a couple of posts.

I’ve been successfull with adding

{{.Store.Delete "icons" }}

to baseof or home (I have no pages and sections in my test site -

one could that wrap in a conditional IsDevelopment or hasShortcode, or delete on retrieval but think we skip that, assuming that deleting a key that’s not present is as fast as checking that before.

not something I can give valuable guidance on.