Static sitemap file
A small, mostly static site needs a conventional sitemap without runtime generation.
Next.js 16+ · Directive
Next.js App Router supports static sitemap.xml files and generated sitemap.ts or sitemap.js files. Generated sitemaps export a default function that returns URL entries for search-engine crawlers.
Add sitemap.ts or sitemap.js at the root of the app directory and export a default function that returns URL entries. Next.js converts those entries into the sitemap response; the route is cached by default unless request-time APIs or dynamic configuration make it dynamic.
export default function sitemap(): MetadataRoute.SitemapA sitemap function returns an array of URL entries; each entry can contain fields such as url, lastModified, changeFrequency, and priority.
export default function sitemap() {
return [
{
url: 'https://acme.com',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
},
]
}Place this file at app/sitemap.ts to generate the site's sitemap from an array of URL entries.
A small, mostly static site needs a conventional sitemap without runtime generation.
A site has dynamic routes whose URLs come from application data.
A large site needs separate sitemap outputs for route groups or data partitions.
Use app/sitemap.xml for a static file, or app/sitemap.ts/app/sitemap.js for a generated sitemap. A generated file exports a default function whose result is an array of URL objects.
Follow-up: How would you split a large sitemap? Use generateSitemaps to return objects with an id, then generate each sitemap from that id.
Each returned entry describes a URL and can include fields such as url, lastModified, changeFrequency, and priority. TypeScript projects can use the documented Sitemap type for the return value.
Follow-up: What route serves a generated split sitemap? It is exposed at the route segment followed by /sitemap/[id].xml.
Multiple sitemap files can be created by nesting sitemap.(xml|js|ts) in different route segments. For programmatic splitting, use generateSitemaps, which returns sitemap identifiers.
Follow-up: What determines the number of generated sitemap URLs? The objects returned by generateSitemaps determine the identifiers and therefore the generated sitemap instances.
generateSitemapsMetadata FilesRoute HandlersSitemaps XML format