Hi, I’m trying to implement a search functionality for my site but I’m having issues generating an index.json file. I’ve looked through multiple tutorials and tried out different templates for the index.json file, but I always get basically just the template as the output.
It doesn’t seem to be an issue with any specific template, all templates that I’ve tried so far are failing. I guess I’m just missing something.
To make things a bit clearer, here is a simplified version of the template I’m trying to use.
{
{- $list := slice -
}
}
{
{- range site.RegularPages.ByPublishDate.Reverse -
}
}
{
{- $item := dict
"title" .Title
-
}
}
{
{- $list = $list | append $item -
}
}
{
{- end -
}
}
{
{- $list | jsonify -
}
}
I have added “JSON” to the outputs of home in my hugo.toml file.
[outputs]
home = [ "HTML", "JSON" ]
To make sure that this actually has content, i tested it by just adding this in the html of a test site and it works - so it doesn’t seem like the problem is with the content.
However, after building, in my public directory the content of the generated index.json file is literally the same as the template in the layouts folder:
{
{- $list := slice -
}
}
{
{- range site.RegularPages.ByPublishDate.Reverse -
}
}
{
{- $item := dict
"title" .Title
-
}
}
{
{- $list = $list | append $item -
}
}
{
{- end -
}
}
{
{- $list | jsonify -
}
}
It seems like it’s not filled/generated correctly.
I’ve tried out several slightly different templates and I’ve tried it out in a new test hugo site - same results every time.
Just to be sure, I also tried to generate the json just with a minimal setup:
{
"message": "Test output"
}
This is generated correctly.
Here is the output of my hugo env:
hugo v0.136.2+extended linux/amd64 BuildDate=unknown
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.23.2"
github.com/sass/libsass="3.6.6"
github.com/webmproject/libwebp="v1.3.2"
Maybe just as a note, in my index.json the parts where the hugo template is used are also underlined with red squiggly lines - not sure if this is just an issue with the JSON formatting in my editor or if I’m just tackling this whole thing wrong and am missing something crucial.
Thank you for the help in advance