# Hugo inserting scripts into search index json breaking it, possible bug maybe?

**URL:** https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168
**Category:** support
**Created:** [October 30, 2022, 1:18pm UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168 "2022-10-30T13:18:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![benmarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/benmarte/32/5948_2.png) [@benmarte](https://discourse.gohugo.io/u/benmarte)
#### Post date: [October 30, 2022, 1:18pm UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168/1 "2022-10-30T13:18:37Z")

</div>

So I’m using elasticlunr for search and for some reason hugo is inserting 2 script tags to my son which causes it to break.

This is the template I am using for my json file in `layouts/json/single.html`

```auto
{{- $.Scratch.Add "index" slice -}}
{{- range .Site.Pages -}}
{{- $.Scratch.Add "index" (dict "title" .Title "uri" .Permalink "tags" .Params.tags "content" .Plain "section" (.Section | title)) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

```

This is the toml of my page:

```auto
---
type: "json"
url: "search-index.json"
---

```

When I visit the link to my json file I see hugo is appending 2 script tags which is breaking my search index. These are the script tags:

```auto
<script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script><script data-no-instant defer>console.warn('"head" or "body" tag is required in html to append livereload script. As a fallback, Hugo injects it somewhere but it might not work properly.');</script>

```

If I start hugo with livereload disabled everything works fine: `hugo server --disableLiveReload`

Any idea if there is a config setting in order to prevent this or if I am doing something wrong or is this a bug?

Thanks.

---

<div class="post-metadata">

### Author: ![frjo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frjo/32/18626_2.png) [@frjo](https://discourse.gohugo.io/u/frjo)
#### Post date: [October 30, 2022, 2:11pm UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168/2 "2022-10-30T14:11:21Z")

</div>

Is this really a problem? You will ever only see it in development since that is the only time you use `hugo server` and you already found a work around for that.

---

<div class="post-metadata">

### Author: ![benmarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/benmarte/32/5948_2.png) [@benmarte](https://discourse.gohugo.io/u/benmarte)
#### Post date: [October 30, 2022, 2:18pm UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168/3 "2022-10-30T14:18:14Z")

</div>

That’s a fair point I just figured out it worked by disabling live reload, the thing is it was not a problem before so it might be a bug.

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [October 30, 2022, 7:29pm UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168/4 "2022-10-30T19:29:49Z")

</div>

> [@benmarte](#):
>
> it was not a problem before

The behavior changed in v0.102.0 due to:

> <https://github.com/gohugoio/hugo/pull/10155>
>
> Currently, Hugo does not inject \`livereload\` script if html does not contain \`\<h…ead\>\` or \`\<body\>\`.
> This sometimes happens if you create new sites without \`theme\` and it is hard to catch the cause soon.
> 
> This PR:
> 
> \* Inject livereload script even if html does not include \`\<head\>\`, \`\<body\>\`, or \`\<html\>\`
> - Modern browsers execute scripts even if they are outside \`\<html\>\`
> - Some js frameworks (confirmed with vite) inject HRM script without \`\<html\>\` tag
> \* Append warning script to html if \`\<head\>\` or \`\<body\>\` is not in html
> \* Fix bug that livereload cannot be appended to the tags with attrs
> 
> Close #10105

You are currently generating your JSON search index with the HTML [output format](https://gohugo.io/templates/output-formats#output-format-definitions). By default, this output format has the `isHTML` property set to `true`, which (among other things) triggers injection of the livereload script.

Instead, generate your JSON search index with the JSON output format. By default, this output format has the `isHTML` property set to `false`, which prevents injection of the livereload script.

layouts/\_default/search.json

```plaintext
{{- $.Scratch.Add "index" slice -}}
{{- range .Site.Pages -}}
{{- $.Scratch.Add "index" (dict "title" .Title "uri" .Permalink "tags" .Params.tags "content" .Plain "section" (.Section | title)) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify (dict "indent" " ") -}}

```

content/search.md

```plaintext
+++
title = 'Search'
date = 2022-10-30T11:58:38-07:00
draft = false
outputs = ['json']
layout = 'search'
url = "search-index.json"
+++

```

---

<div class="post-metadata">

### Author: ![benmarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/benmarte/32/5948_2.png) [@benmarte](https://discourse.gohugo.io/u/benmarte)
#### Post date: [October 31, 2022, 11:58am UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168/5 "2022-10-31T11:58:36Z")

</div>

@jmooring you are a treasure trove of Hugo knowledge, thanks for always looking out for the hugo community. Have a great day.

---

<div class="post-metadata">

### Author: ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)
#### Post date: [November 2, 2022, 11:58am UTC](https://discourse.gohugo.io/t/hugo-inserting-scripts-into-search-index-json-breaking-it-possible-bug-maybe/41168/6 "2022-11-02T11:58:54Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
