I added sitemap.ts and robots.ts to a fresh Next.js 16 app, starting from the docs examples, and the build passed. Then I looked at what the server sent. The <lastmod> dates changed on every request. When I split the sitemap with generateSitemaps using the Next.js 15 signature, every URL came out as NaN. Neither problem printed an error.

Here are the six traps I hit, the fix for each, and the real build output. I tested everything on Next.js 16.4.6, React 19.3.0 and Node 22 in a project made with npx create-next-app@latest. That template turns on cacheComponents by default, which explains the first two traps.

1. The docs example gives you a new lastmod on every request

The basic example in the docs uses lastModified: new Date(). With Cache Components on, the build passes, but look at the route table:

Route (app) ┌ ○ / ├ ○ /_not-found ├ ○ /robots.txt └ ƒ /sitemap.xml 

ƒ means the sitemap is rendered on every request. Two requests, about a second apart:

<lastmod>2026-11-28T09:35:41.106Z</lastmod> <lastmod>2026-11-28T09:35:42.625Z</lastmod> 

Google's sitemap docs say it uses <lastmod> only when it is "consistently and verifiably" accurate. A date that changes every second is neither. (I hit the same cause with feeds in my RSS post.)

The fix is to store a real date for each page and change it only when that page changes:

// src/lib/site.ts export const SITE_URL = ( process.env.NEXT_PUBLIC_SITE_URL ?? 'https://acme.com' ).replace(/\/$/, '') // Change a date only when that page's content really changes. export const pages = [ { path: '/', updated: '2026-11-20' }, { path: '/pricing', updated: '2026-11-25' }, { path: '/about', updated: '2026-10-02' }, ] 
// src/app/sitemap.ts import type { MetadataRoute } from 'next' import { SITE_URL, pages } from '@/lib/site' export default function sitemap(): MetadataRoute.Sitemap { return pages.map((page) => ({ url: `${SITE_URL}${page.path === '/' ? '' : page.path}`, lastModified: page.updated, })) } 

Now the build shows ○ /sitemap.xml, so the file is prerendered once. I also dropped changeFrequency and priority: the same Google page says Google ignores both.

2. A sitemap built from a CMS calls the CMS on every request

Blog posts usually come from a CMS or an API. Here is a plain fetch helper:

// src/lib/posts.ts (uncached) type Post = { slug: string; updatedAt: string } export async function getPosts(): Promise<Post[]> { const res = await fetch(`${process.env.CMS_URL}/posts`) if (!res.ok) throw new Error(`CMS returned ${res.status}`) return res.json() } 

The build passed and showed ƒ /sitemap.xml again. I pointed CMS_URL at a small local server that logs each hit, ran next start, and requested /sitemap.xml three times. The CMS log showed three hits. Every crawler visit would reach your CMS.

Cache the helper with "use cache":

// src/lib/posts.ts import { cacheLife, cacheTag } from 'next/cache' type Post = { slug: string; updatedAt: string } export async function getPosts(): Promise<Post[]> { 'use cache' cacheLife('hours') cacheTag('posts') const res = await fetch(`${process.env.CMS_URL}/posts`) if (!res.ok) throw new Error(`CMS returned ${res.status}`) return res.json() } 
// src/app/sitemap.ts import type { MetadataRoute } from 'next' import { SITE_URL, pages } from '@/lib/site' import { getPosts } from '@/lib/posts' export default async function sitemap(): Promise<MetadataRoute.Sitemap> { const posts = await getPosts() return [ ...pages.map((page) => ({ url: `${SITE_URL}${page.path === '/' ? '' : page.path}`, lastModified: page.updated, })), ...posts.map((post) => ({ url: `${SITE_URL}/blog/${post.slug}`, lastModified: post.updatedAt, })), ] } 

The build output:

Route (app) Revalidate Expire ... └ ○ /sitemap.xml 1h 1d 

The CMS was called once during the build. Three more requests after next start added no hits, and the response carried x-nextjs-cache: HIT. With the hours profile, a request that arrives more than an hour later triggers a background refresh. cacheTag('posts') lets a CMS webhook refresh it sooner (I covered that in my "use cache" post).

3. generateSitemaps: id is now a Promise<string>

The docs' version history says that since v16.0.0 the id passed to the sitemap function is a promise that resolves to a string. That breaks old code in two ways.

First, the docs example itself does math on the string:

const id = await props.id const start = id * 50000 

next build stops at the type check:

src/app/product/sitemap.ts(12,17): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. Failed to type check. 

Second, and worse, the Next.js 15 signature still passes the type check:

// Next.js 15 style export default async function sitemap({ id, }: { id: number }): Promise<MetadataRoute.Sitemap> { const start = id * 50000 const end = start + 50000 return [{ url: `${SITE_URL}/product/${start}-${end}` }] } 

The build passed with no warning, and all three generated files contained this:

<loc>https://acme.com/product/NaN-NaN</loc> 

At runtime id is a Promise, so the math gives NaN.

The fix is to await it and convert it. Here's a complete version, with a stand-in for the database:

// src/lib/products.ts const TOTAL = 120_000 // stand-in for a database count // Google's limit is 50,000 URLs per sitemap file. export const PER_SITEMAP = 50_000 export async function getProductSitemapIds() { const files = Math.ceil(TOTAL / PER_SITEMAP) return Array.from({ length: files }, (_, id) => ({ id })) } export async function getProducts(offset: number, limit: number) { const end = Math.min(offset + limit, TOTAL) return Array.from({ length: Math.max(end - offset, 0) }, (_, i) => ({ id: offset + i + 1, updatedAt: '2026-11-01', })) } 
// src/app/product/sitemap.ts import type { MetadataRoute } from 'next' import { SITE_URL } from '@/lib/site' import { PER_SITEMAP, getProductSitemapIds, getProducts } from '@/lib/products' export async function generateSitemaps() { return getProductSitemapIds() // [{ id: 0 }, { id: 1 }, { id: 2 }] } export default async function sitemap(props: { id: Promise<string> }): Promise<MetadataRoute.Sitemap> { const id = Number(await props.id) // "1" → 1 const products = await getProducts(id * PER_SITEMAP, PER_SITEMAP) return products.map((product) => ({ url: `${SITE_URL}/product/${product.id}`, lastModified: product.updatedAt, })) } 
├ /product/sitemap/[__metadata_id__] │ ├ ● /product/sitemap/0.xml │ ├ ● /product/sitemap/1.xml │ └ ● /product/sitemap/2.xml 

The three files held 50,000, 50,000 and 20,000 URLs, from /product/1 to /product/120000. Google's limit is 50,000 URLs or 50MB uncompressed per sitemap.

4. Split sitemaps get no index file

After the build I requested the URLs you might expect to exist:

/product/sitemap/0.xml 200 /product/sitemap/2.xml 200 /product/sitemap/3.xml 404 /product/sitemap.xml 404 /sitemap-index.xml 404 /sitemap.xml 200 (only the 5 page and post URLs) 

Next.js does not build a sitemap index for you. Google lets you list several Sitemap: lines in robots.txt, with no limit on how many, so robots.ts can list every file:

// src/app/robots.ts import type { MetadataRoute } from 'next' import { SITE_URL } from '@/lib/site' import { getProductSitemapIds } from '@/lib/products' export default async function robots(): Promise<MetadataRoute.Robots> { const productSitemaps = (await getProductSitemapIds()).map( ({ id }) => `${SITE_URL}/product/sitemap/${id}.xml` ) return { rules: { userAgent: '*', allow: '/' }, sitemap: [`${SITE_URL}/sitemap.xml`, ...productSitemaps], } } 
User-Agent: * Allow: / Sitemap: https://acme.com/sitemap.xml Sitemap: https://acme.com/product/sitemap/0.xml Sitemap: https://acme.com/product/sitemap/1.xml Sitemap: https://acme.com/product/sitemap/2.xml 

While you're here, use absolute URLs everywhere. I tried url: '/about' and Next.js wrote <loc>/about</loc> as is, even with metadataBase set in the root layout. Google asks for fully qualified, absolute URLs, which is why every snippet here builds them from SITE_URL.

5. Previews: use noindex, not Disallow: /

A common trick is to serve Disallow: / on preview and staging builds. Google's noindex docs warn against relying on that: if robots.txt blocks a page, the crawler never sees its noindex rule, and the page can still show up in results when other sites link to it.

So keep previews crawlable and mark their pages noindex instead:

// src/app/layout.tsx import type { Metadata } from 'next' import { SITE_URL } from '@/lib/site' import './globals.css' const isProduction = process.env.SITE_ENV === 'production' export const metadata: Metadata = { metadataBase: new URL(SITE_URL), title: 'Acme', // Previews and staging: crawlable, but never indexed. robots: isProduction ? undefined : { index: false, follow: false }, } export default function RootLayout({ children }: LayoutProps<'/'>) { return ( <html lang="en"> <body>{children}</body> </html> ) } 

A build without SITE_ENV rendered <meta name="robots" content="noindex, nofollow"/>. With SITE_ENV=production npx next build, the tag was gone. The value is read at build time, so set it only in the environment that runs your production build.

6. Static export: add force-static, but only there

With output: 'export', even the simplest robots.ts failed:

Error: export const dynamic = "force-static"/export const revalidate not configured on route "/robots.txt" with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export 

sitemap.ts failed the same way. Adding one line to both files fixed it:

export const dynamic = 'force-static' 

The generateSitemaps file didn't need it. With the uncached getPosts from section 2, out/ then held robots.txt, sitemap.xml with the CMS posts, and product/sitemap/0.xml to 2.xml.

That line only works with cacheComponents off. With it on, the build rejects the line:

Error: Route segment config "dynamic" is not compatible with `nextConfig.cacheComponents`. Please remove it. 

So for a static export, remove cacheComponents from next.config.ts (the template turns it on), then add force-static. In a server deployment with Cache Components, leave it out.

7. Check the output before you ship

None of these traps fails the build, so I check what the server sends. This script has no dependencies. It reads robots.txt, fetches each listed sitemap from your local server, and flags relative <loc> values and <lastmod> dates that change between two requests:

// scripts/check-sitemap.mjs // Usage: node scripts/check-sitemap.mjs http://localhost:3000 const base = process.argv[2] ?? 'http://localhost:3000' const problems = [] const robots = await fetch(`${base}/robots.txt`).then((r) => r.text()) const sitemaps = [...robots.matchAll(/^Sitemap:\s*(\S+)/gim)].map((m) => m[1]) if (sitemaps.length === 0) problems.push('robots.txt lists no Sitemap lines') for (const url of sitemaps) { // Fetch from the local server even if robots.txt has the live domain. const local = base + new URL(url).pathname const first = await fetch(local) if (!first.ok) { problems.push(`${url} → HTTP ${first.status}`) continue } const xml = await first.text() const locs = [...xml.matchAll(/<loc>([^<]+)<\/loc>/g)].map((m) => m[1]) const relative = locs.filter((loc) => !/^https?:\/\//.test(loc)) if (relative.length) problems.push(`${url}: relative <loc> ${relative[0]}`) // lastmod must not change between two requests. await new Promise((r) => setTimeout(r, 1100)) const again = await fetch(local).then((r) => r.text()) const mods = (s) => [...s.matchAll(/<lastmod>([^<]+)/g)].map((m) => m[1]).join() if (mods(xml) !== mods(again)) problems.push(`${url}: <lastmod> changes per request`) console.log(`${url}: ${locs.length} URLs`) } if (problems.length) { console.error('\n' + problems.map((p) => `✗ ${p}`).join('\n')) process.exit(1) } console.log('\n✓ sitemaps OK') 

Run it against next start after a production build. On the fixed project:

https://acme.com/sitemap.xml: 5 URLs https://acme.com/product/sitemap/0.xml: 50000 URLs https://acme.com/product/sitemap/1.xml: 50000 URLs https://acme.com/product/sitemap/2.xml: 20000 URLs ✓ sitemaps OK 

With a docs-style new Date() sitemap that also had one relative URL, it exits with code 1:

✗ https://acme.com/sitemap.xml: relative <loc> /about ✗ https://acme.com/sitemap.xml: <lastmod> changes per request 

Wrapping up

The short version for Next.js 16:

  1. Use real dates in lastModified, not new Date().
  2. Put "use cache" on the helpers that fetch sitemap data.
  3. In generateSitemaps files, write Number(await props.id).
  4. List split sitemaps in robots.ts, since there's no index file.
  5. Use absolute URLs only.
  6. Mark previews noindex; don't block them in robots.txt.
  7. Add force-static for static export, and nowhere else.

Do you split your sitemaps with generateSitemaps, or does one file cover your whole site?

Disclosure

I sell Next.js Launch Skills, 13 agent skills for Claude Code, Cursor and Codex that take a Next.js site from build to launch, with zero-dependency check scripts. Its launch-seo skill covers metadata, canonical URLs, sitemap, robots and JSON-LD, plus a crawler that checks every page. It's $9.99. There's also a free MIT edition: npx skills add nandot12/nextjs-launch-skills · GitHub