Repeated json entries

Sometime when I generate a index.json I get repeated entries and I have to:

  • empty the index.json template file
  • save
  • restart the server
  • quit the server
  • add everything inside the index.json template again
  • restart the server

Otherwise, I get repeated entries.

{{- $.Scratch.Add "index" slice -}}
{{- range where .Site.RegularPages "Type" "not in"  (slice "page" "json") -}}
    {{- $.Scratch.Add "index" (dict "title" .Title "tags" .Params.tags "categories" .Params.categories "contents" .Summary "permalink" .RelPermalink) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

Nothing weird there…

What do you mean by repeated entries? All entries twice or single entries that come up twice?

I mean all entries repeat a few times (not twice but, up to five or six times, either is random or related to the amount of entries I have, no idea)

The behavior in detail:

  1. Fire up the Hugo Server
  2. Go to /index.json to look at the entries. All entries seem fine.
  3. Go to the website index or to any other page within the website.
  4. Go back to the /index.json to look at the entries. The mess. They are all repeated. A single entry can be repeated like ten, or twenty times, so they add up and I have 567 or so entries when I should only have 20.

My config file:

baseURL = ""
relativeURLS = "true"
languageCode = "en-us"
title = "Cabrera Brothers Archive"
theme = "archives"
uglyURLs = "true"
sectionPagesMenu = "main"
Paginate = "4"

  home = ["HTML", "JSON"]
  page = [ "HTML"]

    ordered = "true"
    endLevel = 4

Me again,

It resolved by using this json instead of the one I had before. To be honest, I have no idea why the one I had before was giving me such a hard time.

[{{ range $index, $page := .Site.RegularPages }}
    {{- if ne $page.Type "json" -}}
    {{- if and $index (gt $index 0) -}},{{- end }}
        "permalink": "{{ $page.RelPermalink }}",
        "title": "{{ htmlEscape $page.Title}}",
        "tags": [{{ range $tindex, $tag := $page.Params.tags }}{{ if $tindex }}, {{ end }}"{{ $tag| htmlEscape }}"{{ end }}],
        "categories": "{{ $page.Params.categories }}",
        "contents": {{ $page.Summary | jsonify }}
    {{- end -}}
{{- end -}}] 

I have the same problem with this similar code

{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
    {{- $.Scratch.Add "index" (dict "title" .Title "tags" .Params.tags "categories" .Params.categories "contents" .Plain "permalink" .Permalink) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

Im leaning towards the this line {{- $.Scratch.Add "index" slice -}} is responsible. Could it be that some internals storage for scratch is not cleared so that this ad in the beginning just add a new slice to a existing index from the last build?

I tried adding {{- $.Scratch.Delete "index" -}} and hope it resolves it.

Do you need to use a scratch? Why not:

{{ $index := slice }}
{{ range .Site.RegularPages }}
  {{ $index = $index | append (dict "title" .Title "tags" .Params.tags "categories" .Params.categories "contents" .Plain "permalink" .Permalink) }}
{{ end }}
{{ $index | jsonify }}

I probably dont, this was just some code I found in a guide to generate a search index. I tried yours and it seem to work just fine.

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.