Structured data and pagination

Hi all,

Recently Google Search Console reported problem with one of my page.
I used partial for structured data that was copied from some layout. It looks like this:

{{- $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" -}}
{{- $.Scratch.Add "path" .Site.BaseURL -}}

{{- $.Scratch.Add "breadcrumb" (slice (dict "url" .Site.BaseURL "name" "Main page" "position" 1 )) -}}
{{- range $index, $element := split $url "/" -}}
{{- $.Scratch.Add "path" $element -}}
{{- $.Scratch.Add "path" "/" -}}
{{- if ne $element "" -}}
{{- $.Scratch.Add "breadcrumb" (slice (dict "url" ($.Scratch.Get "path") "name" . "position" (add $index 2))) -}}
{{- end -}}
{{- end -}}
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{{ range $.Scratch.Get "breadcrumb" }}{{ if ne .position 1 }},{{ end }}{
		"@type": "ListItem",
		"position": {{ .position }},
		"item": {
		  "@id": "{{ .url }}",
		  "name": "{{ .name }}"
		}
	}{{ end }}]
}
</script>

Everything is ok except situation when I use pagination. When I go to second page (ur: https://mypage.com/page/2/) the generated structured data looks like this:

{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
		"@type": "ListItem",
		"position": 1,
		"item": {
		  "@id": "https://mypage.com",
		  "name": "Main page"
		}
	}{
		"@type": "ListItem",
		"position": 1,
		"item": {
		  "@id": "https://mypage.com",
		  "name": "Main page"
		}
	}]
}
As you can see this structure is incorrect. The part of this structure was duplicated and comma between this parts is missing. Can you give me some advice how to fix it? I want generate only
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
		"@type": "ListItem",
		"position": 1,
		"item": {
		  "@id": "https://mypage.com",
		  "name": "Main page"
		}
	}

for my home page and other where pagination is used.

Thanks in advance for help.

You could “debug” the issue by placing the following code outside of the script, load the page and copy paste the output here:

<!--
{{ range $.Scratch.Get "breadcrumb" }}
{{.position }}: {{ .url }} - {{ .name }}
{{end}}
//-->

Could it be that your site is inside of a subfolder on your domain, like http://domain.com/hugo/index.html? I think he counts your site root twice. (he = scratch)

In that case, I would add the home link in the start of the site static, then remove the part up to there from the URL and let the rest go through your scratch routine.

Thank you very much for useful clues