How would I go about omitting certain pages from the generated sitemap? I understand that I can create my own template to override the built in one for sitemap.xml, but is there a way to exclude certain pages?
I only ask because I’ve written a conditional script that determines whether or not my analytics should be loaded, based on whether a localstorage key & value are set in the browser. I wanted to have a page accessible on my site that I could open up before working on the site to place the key, and thus keep my browsing/development activity from polluting my analytics data.
I could just manually place the key into localstorage on each browser I use, but it’s a little messy; as few browsers allow manual addition of data without extensions or plugins, and the key-setting page would allow me to work from any computer/browser, without the need for excluding IPs (plus my home network uses a dynamic one anyway…)
Obviously, I want this page to only be accessible to me, although it’s not the end of the world if someone stumbles onto it. I just don’t want it indexed for search engines and similar.
For reference to others looking for the solution to this, here’s my code. I used the boolean “private” on pages I didn’t want included in the sitemap, then set my sitemap.xml layout as the following. It’s the standard, suggested layout, with the addition of my conditional check.
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ range .Data.Pages }}
{{ if not .Params.private }}
<url>
<loc>{{ .Permalink }}</loc>
<lastmod>{{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}
</url>
{{ end }}
{{ end }}
</urlset>