Hide pages from Hugo generated sitemap

28 May 2021 3 min read webdev

Want ‘hidden’ pages? Override Hugo’s default sitemap.xml behaviour by making your own sitemap.xml file inside layouts/_default/.

You can copy Hugo’s default sitemap.xml and modify it to check for a Frontmatter tag of sitemapExclude: true. While I was there, I also told Hugo to only include “regular pages” in my sitemap. So no tags, categories, author pages, etc. My full sitemap.xml contents is below.

{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">
  {{ range $.Site.RegularPages }}
   {{- if ne .Params.sitemapExclude true -}}
    {{- if .Permalink -}}
  <url>
    <loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
    <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
    <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
    <priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
    <xhtml:link
                rel="alternate"
                hreflang="{{ .Language.Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
    <xhtml:link
                rel="alternate"
                hreflang="{{ .Language.Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
  </url>
    {{- end -}}
   {{- end -}}
  {{ end }}
</urlset>

I also noticed that Hugo didn’t seem to use the changeFreq or priority values of the Sitemap protocol as I expected. You need to manually do this if you want to use those values:

In your config.toml, add in the sitemap defaults:

[sitemap]
  changefreq = "monthly"
  filename = "sitemap.xml"
  priority = 0.5

And to override per page, you must use this exact format within the page’s frontmatter:

sitemap:
  changefreq: "daily"
  priority: 1
comments powered by Disqus