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"
  {{ range $.Site.RegularPages }}
   {{- if ne .Params.sitemapExclude true -}}
    {{- if .Permalink -}}
    <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 }}
                hreflang="{{ .Language.Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
                hreflang="{{ .Language.Lang }}"
                href="{{ .Permalink }}"
                />{{ end }}
    {{- end -}}
   {{- end -}}
  {{ end }}

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:

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

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

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