Adding schema from data files

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?

In the code you posted, where are you inserting the warnf call?

I did a similar call in the second code sample but took it out while editing the post but the warnf logic is placed before the schema should be inserted to check the urls are valid along with the values being placed in the jsonld schema.

example:

 hugo -b https://www...
Start building sites … 
hugo v0.118.2+extended linux/amd64 BuildDate=2023-10-11T13:53:35Z VendorInfo=debian:0.118.2-1


WARN  INSERTING SCHEMA FOR https://www.... duration: PT9M
WARN  INSERTING SCHEMA FOR https://www.... duration: PT9M1S
WARN  INSERTING SCHEMA FOR https://www.... duration: PT9M1S
WARN  INSERTING SCHEMA FOR https://www.... duration: PT9M1S
WARN  INSERTING SCHEMA FOR https://www.... duration: PT8M
WARN  INSERTING SCHEMA FOR https://www.... duration: PT1H56M5S



                   |  EN   
-------------------+-------
  Pages            |  108  
  Paginator pages  |   46  
  Non-page files   |  104  
  Static files     | 3143  
  Processed images |    0  
  Aliases          |    3  
  Sitemaps         |    1  
  Cleaned          |    0  

Total in 1518 ms

after building the site, no

I’ll look at it if you want to share your project.

i’ve said multiple times sharing this project is not an option.

if the code samples are not enough what will sharing the project do?

All of this is being performed in baseof.html template.

I have other schema that gets built. This particular schema is built and rendered if hugo server is used but hugo -b fails…

Encourage me to spend time on it.