My atom feed broke and I'm not sure why; .Data.Pages not producing list of posts?

At some point in the past I had configured an atom feed for my blog. In config/_default/config.yml I have:

outputs:
  home:
    - HTML
    - RSS
    - Atom

outputFormats:
  RSS:
    mediatype: "application/rss+xml"
    baseName: "rss"
  Atom:
    mediatype: "application/atom+xml"
    baseName: "atom"

mediaTypes:
  application/rss+xml:
    suffixes:
      - xml
  application/atom+xml:
    suffixes:
      - xml

This results in files rss.xml and atom.xml. The RSS feed seems fine, but the atom feed no longer includes any posts. In layouts/_default/list.atom.xml I have:

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>{{ .Site.Title }}</title>

  {{ with .OutputFormats.Get "Atom" }}
  <link rel="self" href="{{ .Permalink }}"/>
  {{ end }}
  <link href="{{ .Permalink }}" rel="alternate"></link>

  <updated>{{ .Date.Format "2006-01-02T15:04:05Z" | safeHTML }}</updated>
  <id>{{ .Permalink }}</id>
  {{ range first 15 .Data.Pages }}
  <entry>
    <title>{{ .Title }}</title>
    <author>
      <name>{{ .Params.author | default .Site.Author.name }}</name>
    </author>
    <link rel="alternate" href="{{ .Permalink }}"/>
    <id>{{ .Permalink }}</id>
    <published>{{ .Date.Format "2006-01-02T15:04:05Z" | safeHTML }}</published>
    <updated>{{ .Lastmod.Format "2006-01-02T15:04:05Z" | safeHTML }}</updated>
    <summary type="html">{{ .Summary | html }}</summary>
  </entry>
  {{ end }}
</feed>

I’m confident that at some point this worked, but now the the {{ range first 15 .Data.Pages }} expression only produces entries for “Abouts” and “Archives”. What’s going on here?

You need to range through .RegularPages. See the internal RSS template:

@jmooring thanks for the pointer. I’m not getting good results with .RegularPages either. If I simplify the template to look like this:

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>{{ .Site.Title }}</title>

  {{ range first 15 .RegularPages }}
  <entry>
    <title>{{ .Title }}</title>
  </entry>
  {{ end }}
</feed>

…then I get no <entry>...</entry> sections. If I try using .Data.RegularPages, it just blows up (Building sites … ERROR 2020/07/24 13:23:57 render of "taxonomy" failed: "..l.ist.atom.xml:4:11": execute of template failed: template: _default/list.atom.xml:4:11: executing "_default/list.atom.xml" at <first 15 .Data.RegularPages>: error calling first: both limit and seq must be provided).

Look carefully at the logic in the internal RSS template:

{{- $pctx := . -}}
{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}}
{{- $pages := slice -}}
{{- if or $.IsHome $.IsSection -}}
{{- $pages = $pctx.RegularPages -}}
{{- else -}}
{{- $pages = $pctx.Pages -}}

For the home page, it ranges through .Site.RegularPages.

For section pages, it ranges through .RegularPages.

For everything else it ranges through .Pages.

For the home page, it ranges through .Site.RegularPages .

For section pages, it ranges through .RegularPages .

For everything else it ranges through .Pages .

Whoops, I missed the bit about `.Site.RegularPages; that seems to be the correct value. What I wonder if whether or not the existing template ever worked, or if I just never checked it until recently. Anyway, thanks!

https://discourse.gohugo.io/t/missing-posts-in-subdirs-with-hugo-0-57/21769/2

1 Like

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