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 }}