# Sanity XML sitemap

> Query page documents with GROQ, build absolute URLs and real lastmod dates in your frontend, and fix the defaults in Sanity

Canonical: https://scaplet.com/seo/sanity/xml-sitemap

Guide Sanity Sanity · Crawling

Query page documents with GROQ, build absolute URLs and real lastmod dates in your frontend, and fix the defaults in Sanity's own sitemap lesson.

3 min

read

5

sources

Oct 29, 2026

updated

Scaplet Editorial Team

author

In one line

Sanity does not output a sitemap; your frontend builds one from documents. Write a GROQ query that returns each public document's path and `_updatedAt`, map it to absolute canonical URLs in a sitemap route, and return the XML. Sanity's own lesson works as a base but sets `priority` and `changeFrequency`, which Google ignores, and builds the origin from a deployment URL.

30-second check

## Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?

Summarize this page with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fsanity%2Fxml-sitemap%20(%22Sanity%20XML%20sitemap%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fsanity%2Fxml-sitemap%20(%22Sanity%20XML%20sitemap%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fsanity%2Fxml-sitemap%20(%22Sanity%20XML%20sitemap%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fsanity%2Fxml-sitemap%20(%22Sanity%20XML%20sitemap%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 3 parts, 3 minutes.

1. [Start from Sanity's lesson, then fix three things 01](#start-from-sanity-s-lesson-then-fix-three-things)
2. [Keep drafts and non-public documents out 02](#keep-drafts-and-non-public-documents-out)
3. [Check it worked 03](#check-it-worked)

_01_What to change in the lesson's code

## Start from Sanity's lesson, then fix three things

Sanity Learn's "Build a dynamic sitemap" lesson fetches every `page` and `post` document with a GROQ query and returns the shape that the Next.js App Router sitemap route expects. The query is a good model for any frontend.

In the lesson

### `changeFrequency: "weekly"`, `priority: 1` on every URL

Problem

Google says it ignores `<priority>` and `<changefreq>`

Do this instead

Drop both fields

In the lesson

### `process.env.VERCEL_URL` as the base URL

Problem

Vercel documents `VERCEL_URL` as the domain of the generated deployment URL, such as `*.vercel.app`, and without the protocol

Do this instead

Hard-code or configure your production origin, for example `https://www.example.com`

In the lesson

### `_updatedAt` as `lastModified`

Problem

Google uses [lastmod](/glossary/lastmod) only if it is consistently and verifiably accurate

Do this instead

Keep it where it tracks page changes; omit it where it does not

In the lesson

### `return []` in the `catch` block

Problem

A failed fetch is logged with `console.error` and then publishes an empty sitemap

Do this instead

Rethrow the error or fail the build so an empty sitemap never ships

Query from the lesson

```
*[_type in ["page", "post"] && defined(slug.current)] { "href": select( _type == "page" => "/" + slug.current, _type == "post" => "/posts/" + slug.current, slug.current ), _updatedAt }
```

The lesson also keeps one query for two document types. Add a type only when it has a public route, and make sure the path logic matches your router.

_02_Worth knowing

## Keep drafts and non-public documents out

Sanity's perspectives documentation says the `published` perspective excludes all unpublished changes from results, while `raw` returns drafts, versions and published content side by side for authenticated requests. Set `perspective: 'published'` on the client that powers the sitemap so a draft-only page cannot slip in.

### Add an indexing field

If an editor can mark a page `noindex`, filter it in the query (for example `&& noindex != true`) so the sitemap never lists a URL that tells Google to skip it.

### Mirror your routes

If a document redirects elsewhere, its old path should not appear. Pair the sitemap query with your [Sanity redirects](/seo/sanity/redirects) list.

### Split at the limit

Google caps one sitemap at 50MB (uncompressed) or 50,000 URLs; Sanity's lesson repeats the 50,000 URL cap. Beyond that, create several sitemaps and a [sitemap index](/glossary/xml-sitemap).

Client for the sitemap route (example)

```
import { createClient } from '@sanity/client' export const sitemapClient = createClient({ projectId: '<your-project-id>', dataset: '<your-dataset>', apiVersion: '2023-05-03', useCdn: false, perspective: 'published', })
```

_03_How it works

## Check it worked

1. 1

### Open the file

Visit `/sitemap.xml` on the deployed site. Every `<loc>` should start with your production origin, not `localhost` or a `.vercel.app` host.
2. 2

### Compare counts

Run `count(*[...])` as a GROQ query with the same filter as the sitemap query and compare the result with the number of `<url>` entries.
3. 3

### Publish a change

Publish a page in Studio and confirm its URL appears after your site's next rebuild or revalidation. Unpublish another and confirm it disappears.
4. 4

### Submit it

Add the sitemap URL under Sitemaps in [Search Console](/glossary/google-search-console) and read the status. Sanity's [glossary](/glossary/glossary-page) also advises referencing the file in `robots.txt`.

**Review sitemap changes before they ship**Scaplet connects to Sanity and Search Console, can audit your sitemap against live status codes and canonicals, and sends every change through approval so it can be undone. Run the free audit at /site-audit.

Sources · 5

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- sanity.io
- google.com
- vercel.com

1. 01 [Build a dynamic sitemap | Sanity Learn](https://www.sanity.io/learn/course/seo-optimization/building-a-dynamic-sitemap) sanity.io · checked 2026-11-01
2. 02 [Sitemap.xml definition | Sanity Glossary](https://www.sanity.io/glossary/sitemapxml) sanity.io · checked 2026-11-01
3. 03 [Perspectives | Sanity Docs](https://www.sanity.io/docs/content-lake/perspectives) sanity.io · checked 2026-11-01
4. 04 [Build and submit a sitemap | Google Search Central](https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap) developers.google.com · checked 2026-11-01
5. 05 [System environment variables | Vercel Docs](https://vercel.com/docs/environment-variables/system-environment-variables) vercel.com · checked 2026-11-01

Questions

## Asked a lot.

2 questions people ask next, answered from the sources on this page.

_01_ Does Sanity generate sitemap.xml automatically?

No. Sanity stores documents and answers GROQ queries. Your frontend turns that query result into a sitemap route or generated file.

_02_ Should I include priority and changefreq?

No. Google says it ignores both values, so they add nothing for Google and only create things to maintain.

## Keep learning

[All platform guides](/seo)

[Learn Sanity redirects Headless CMS](/seo/sanity/redirects) [Related Canonical URL Crawling and indexing](/glossary/canonical-url) [Read next Sanity SEO Headless CMS](/seo/sanity) [Do Run a free site audit Free, about two minutes](/site-audit)

Nearby in platform guides
[Shopify SEO](/seo/shopify) [Shopify 404 page](/seo/shopify/404)

More in Sanity SEO
[Sanity image SEO](/seo/sanity/image-seo) [Sanity redirects](/seo/sanity/redirects) [Sanity structured data](/seo/sanity/structured-data) [Sanity XML sitemap](/seo/sanity/xml-sitemap)

Terms on this page
[lastmod](/glossary/lastmod) [XML sitemap](/glossary/xml-sitemap) [Google Search Console](/glossary/google-search-console) [Glossary page](/glossary/glossary-page)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

Run the free audit, then approve the fixes. Every change can be undone.

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
