Skip to content

Syntax Corruption and Crash in index.json Search Template #556

Description

@aniket866

Describe the bug

Description

The search index layout file manually prints braces, quotes, keys, and values to build index.json, but fails to place commas between items in the page range loop, or prints trailing commas when pages are excluded.

  • File Link: themes/microcks/layouts/_default/index.json
  • Code Snippet:
    }{{- if eq $item $len -}}{{- else -}},{{- end -}}

Why It Breaks

Without proper comma delimiters between items in the inner loop, the result is malformed JSON. Furthermore, if a page is excluded (e.g., ignoreSearch: true), the hardcoded comma rendering logic results in trailing commas (e.g. [ { ... }, ]) or consecutive commas (e.g., ,,). This causes JSON.parse() to throw a syntax error in the browser and completely breaks the search capability.

How to Reproduce

  1. Configure any documentation page with ignoreSearch = true in frontmatter.
  2. Build the Hugo site: hugo.
  3. Open the output file public/index.json and validate it using a JSON validator (like JSON.parse() in Node.js).
  4. Observe the parsing failure due to syntax error.

Reproduction Flow

graph TD
    A[Start: Build Search Index] --> B[Hugo iterates pages in Group]
    B --> C{Is page ignored?}
    C -- Yes --> D[Page skipped; index incremented]
    C -- No --> E[Object printed]
    D --> F[Prints delimiter comma anyway]
    F --> G[Generates trailing comma at end of list: ', ]']
    G --> H[Client script fetches index.json]
    H --> I[Execute JSON.parse()]
    I --> J[Error: SyntaxError: Unexpected token ] in JSON]
Loading

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions