Next.js · Junior · Question 52 of 75
How do you generate sitemap.xml in the App Router?
// app/sitemap.ts
import type { MetadataRoute } from 'next'
export default function sitemap(): MetadataRoute.Sitemap {
return [{ url: 'https://acme.com', lastModified: new Date() }]
}- ✓Correct answer: Add
app/sitemap.ts(or a staticsitemap.xml) that returns the list of URLs - 2Install a plugin; Next.js has no built-in support
- 3Put the URLs into
metadata.sitemapin the root layout
Explanation
sitemap is a special metadata file. Next.js serves it at /sitemap.xml; the function can fetch URLs from a database or CMS.