Adding application/json resources to Content Adapter pages

Hi,

I would like to add resource of mediaType application/json to pages generated by a Content Adapter and I took the following route

{{ $isoLanguages := hugo.Data.languages.languages }}
{{- $byLanguage := dict }}
{{- with resources.Match "meta/*.json" }}
	{{- range . }}
		{{- $meta := . | transform.Unmarshal }}
        {{- $lang := "" }}
        {{- with $meta.original_language }}
            {{- $lang = (index $isoLanguages .).english_name }}
            {{- $metaVal := dict "title" $meta.fcg_title "poster_path" $meta.poster_path "backdrop_path" $meta.backdrop_path}}
            {{- with index $byLanguage $lang }}
                {{- $newVal := append $metaVal . }}
                {{- $byLanguage = merge $byLanguage (dict $lang $newVal) }}
            {{- else }}
                {{- $byLanguage = merge $byLanguage (dict $lang (slice $metaVal)) }}
            {{- end }}
        {{- end }}
	{{- end }}
{{- end }}

{{- range $lang, $metaVal := $byLanguage }}
    {{ $content := dict "mediaType" "text/markdown" "value" $lang }}
    {{ $page := dict
        "title" $lang
        "path" (urlize $lang)
        "kind" "page"
        "content" $content
    }}
    {{- $.AddPage $page }}

    {{- $jsonContent := dict "mediaType" "application/json" "value" (jsonify $metaVal) }}
    {{- $jsonResource := dict 
            "name" (printf "%s-json" $lang)
            "content" $jsonContent
            "path" (printf "%s/data.json" $lang)
    }}
    {{- $.AddResource $jsonResource }}
{{- end }}

I see that the resource data.json is correctly generated inside the public folder. But when I try access the resources from my template:

{{- define "main" }}
    {{- with .Resources.Get (printf "%s/data.json" (urlize .LinkTitle)) }}
        {{- range sort (transform.Unmarshal .Content) "title" }}
            <pre>{{- debug.Dump .  }}</pre>
        {{- end }}
    {{- end }}
{{- end }}

the resource is not found. However when I access the resource by name like this:

{{- define "main" }}
    {{- with .Resources.Get (printf "%s-json" .LinkTitle) }}
        {{- range sort (transform.Unmarshal .Content) "title" }}
            <pre>{{- debug.Dump .  }}</pre>
        {{- end }}
    {{- end }}
{{- end }}

the resource is found and the template correctly generated. Would someone help me understand why accessing the resource by file name fails but accessing it by name works. As a related question, I find that the added resources are placed in public, not in the assets folder where I normally place my resources.

Just wanted to add that I have tried different path attributes to the resource (relative, absolute, relative to page etc.) but none seem to work. Only accessing by the name attribute works.

Thank you.

you assigned a name to the page resource, so it takes the name instead of the file path.

from Page resources

name
(string) Sets the value returned by Name. Supports the :counter placeholder. After assignment, use name, not the original file path, with Resources.Get, Resources.Match, and Resources.GetMatch.

The content adapter creates Site contentpages and page resources

from: Content adapters

AddResource
Adds a page resource to the site.

Thank you! Will read the fine documentation next time. :slight_smile: