Use the canonical production URL
Do not put preview hosts, localhost, duplicate host variants, or redirected URLs in the sitemap.
Build a Next.js App Router sitemap with the metadata route, split large URL sets, and verify canonical URLs and sitemap limits.
For an App Router site, add app/sitemap.ts and return a MetadataRoute.Sitemap array of absolute URLs. For a small site, a static app/sitemap.xml also works. Split large URL sets before they exceed Google's 50,000 URL or 50 MB uncompressed limits, and include only canonical URLs you want crawled; a sitemap helps discovery but does not guarantee indexing.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
app/sitemap.xml file for smaller applications and a special app/sitemap.ts file when the URL list comes from your app. The metadata route returns a MetadataRoute.Sitemap array and is available at /sitemap.xml. Generate absolute URLs from the production site origin, and source the list from pages that are public and intended for search.Replace getPublishedPages() with your CMS query. This example assumes it returns published, indexable pages and a meaningful update date. If your app uses localized URLs, Next.js supports language alternates on sitemap entries; generate each canonical locale URL from the same source of truth.
import type { MetadataRoute } from 'next'
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const pages = await getPublishedPages()
return pages.map((page) => ({
url: `https://example.com/${page.slug}`,
lastModified: page.updatedAt,
}))
}Do not put preview hosts, localhost, duplicate host variants, or redirected URLs in the sitemap.
Drafts, internal search results, account pages, and duplicate filtered views generally do not belong in a public discovery list.
Next.js serializes sitemap metadata such as last modification dates. Google uses lastmod when it is consistently and verifiably accurate; do not stamp every URL with the current build date if the page did not change.
Google says it ignores these optional values.
A sitemap can expose an orphan, but it does not make that page useful or guarantee it will be indexed.
Google supports the sitemap formats defined by the Sitemaps protocol and says each has its own benefits, so pick the one that fits your site. Google calls submitting a sitemap a hint: it does not guarantee that Google will download the sitemap or use it for crawling. Submit the file in Search Console and review parsing errors; the URL Inspection tool can help with a specific page, while the sitemap report is for the submitted file.
generateSitemaps for generating multiple sitemap files. For a growing catalog, partition by stable content groups such as products, articles, and locations, and keep each generated file comfortably below both limits.Keep partition IDs deterministic so a deploy does not unpredictably move URLs between files. Put the file where its URLs should live: Next.js serves the generated sitemaps at /.../sitemap/[id].xml, for example /product/sitemap/1.xml for app/product/sitemap.ts. Since Next.js 16 the id is passed to the sitemap function as a promise that resolves to a string; earlier versions pass a plain string, and await works on both. The Next.js docs do not describe generating a sitemap index, so list each file in Search Console or in robots.ts (its sitemap field accepts an array), or write an index yourself. Google says you can submit multiple sitemaps and index files.
import type { MetadataRoute } from 'next'
export async function generateSitemaps() {
const count = await getSitemapPartitionCount()
return Array.from({ length: count }, (_, id) => ({ id }))
}
export default async function sitemap(props: {
id: Promise<string>
}): Promise<MetadataRoute.Sitemap> {
const id = Number(await props.id)
const pages = await getPublishedPagesForPartition(id)
return pages.map((page) => ({
url: `https://example.com/${page.slug}`,
lastModified: page.updatedAt,
}))
}Visit https://example.com/sitemap.xml and confirm it returns XML with absolute production URLs, not an HTML error page.
Choose one page from each data source or partition. Confirm it returns a successful response, has the intended canonical, is not blocked from crawling, and is intended for indexing.
Count the URLs and check uncompressed size for each sitemap. Compare lastmod with actual content updates rather than deployment time.
Add the sitemap in Search Console. Review the sitemap report for read or parsing issues, then inspect individual important URLs separately.
If you use the Pages Router, verify its route conventions separately; this guide covers the App Router metadata file. Continue with Next.js redirects, the Google Search Console sitemap submission guide, and Next.js robots.txt for discovery and crawling checks.
3 questions people ask next, answered from the sources on this page.
For the App Router, place a static sitemap at app/sitemap.xml or return a generated sitemap from app/sitemap.ts. Next.js exposes the generated metadata route at /sitemap.xml.
Google allows up to 50,000 URLs or 50 MB uncompressed per sitemap. Split the URL set into multiple files and use a sitemap index when needed.
No. A sitemap helps Google discover URLs, but submission does not guarantee crawling or indexing. Keep the page useful, canonical, crawlable, and linked from relevant pages.
Run the free audit, then approve the fixes. Every change can be undone.