I use youtube api to get videoObject schema data and save it in data/videos
{{ $regularPages := .Site.RegularPages }}
{{ $currentPageURL := .Permalink }}
{{ $videoData := .Site.Data.videos }}
{{ $matchedData := dict }}
{{ $baseURL := .Site.BaseURL }}
{{ range $videoData }}
{{ $shouldIncludeSchema := false }}
{{ $modifiedURL := "" }}
{{ if strings.Contains .url "posts" }}
{{ $modifiedURL = replace .url "posts" "blog" }}
{{ end }}
{{ $fullVideoURL := printf "%s%s" $baseURL $modifiedURL }}
{{/* {{ printf "Comparing: %s >== %s" $fullVideoURL $currentPageURL }}<br> */}}
{{ if eq $fullVideoURL $currentPageURL }}
{{ $shouldIncludeSchema = true }}
{{ warnf "INCLUDE SCHEMA: %s with %s" $fullVideoURL $currentPageURL }}
{{ end }}
{{ if $shouldIncludeSchema }}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "VideoObject",
"name": "{{ .title }}",
"description": "{{ .description }}",
"thumbnailUrl": [
"https://img.youtube.com/vi/{{ .video_id }}/default.jpg",
"https://img.youtube.com/vi/{{ .video_id }}/mqdefault.jpg",
"https://img.youtube.com/vi/{{ .video_id }}/hqdefault.jpg",
"https://img.youtube.com/vi/{{ .video_id }}/sddefault.jpg",
"https://img.youtube.com/vi/{{ .video_id }}/maxresdefault.jpg"
],
"uploadDate": "{{ .uploadDate }}",
"duration": "{{ .duration }}",
"contentUrl": "{{ .contentUrl }}",
"embedUrl": "{{ .embedUrl }}",
"interactionStatistic": {
"@type": "InteractionCounter",
"interactionType": { "@type": "WatchAction" },
"userInteractionCount": {{ .interactionStatistic.userInteractionCount }}
},
"regionsAllowed": "{{ .regionsAllowed }}"
}
</script>
{{ end }}
{{ end }}
the above code loops through the saved data returned from the api call.
This code is included in baseof.html and it works just fine when running hugo server;
when I actually build the site, the schema doesn’t get added to the html file.
i’ve tried this, to check when it’s not server running:
{{ if not $.Site.IsServer }}
{{ $baseURL := .Site.BaseURL }}
{{ $videoData := .Site.Data.videos }}
{{ range .Site.RegularPages }}
{{ $currentPageURL := .Permalink }}
{{ range $vdata := $videoData }}
{{ $modifiedURL := "" }}
{{ if strings.Contains $vdata.url "posts" }}
{{ $modifiedURL = replace $vdata.url "posts" "blog" }}
{{ end }}
{{ $fullVideoURL := printf "%s%s" $baseURL $modifiedURL }}
{{ if eq $fullVideoURL $currentPageURL }}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "VideoObject",
"name": "{{ $vdata.title }}",
"description": "{{ $vdata.description }}",
"thumbnailUrl": [
"https://img.youtube.com/vi/{{ $vdata.video_id }}/default.jpg",
"https://img.youtube.com/vi/{{ $vdata.video_id }}/mqdefault.jpg",
"https://img.youtube.com/vi/{{ $vdata.video_id }}/hqdefault.jpg",
"https://img.youtube.com/vi/{{ $vdata.video_id }}/sddefault.jpg",
"https://img.youtube.com/vi/{{ $vdata.video_id }}/maxresdefault.jpg"
],
"uploadDate": "{{ $vdata.uploadDate }}",
"duration": "{{ $vdata.duration }}",
"contentUrl": "{{ $vdata.contentUrl }}",
"embedUrl": "{{ $vdata.embedUrl }}",
"interactionStatistic": {
"@type": "InteractionCounter",
"interactionType": { "@type": "WatchAction" },
"userInteractionCount": {{ $vdata.interactionStatistic.userInteractionCount }}
},
"regionsAllowed": "{{ $vdata.regionsAllowed }}"
}
</script>
{{ end }}
{{ end }}
{{ end }}
{{ end }}
i use warnf and its clear the code is being called both in server mode and when hugo build is called but when hugo build the schema is not written to the html.
Any help figuring out why and possibly how to fix it?