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() }]
}

Explanation

sitemap is a special metadata file. Next.js serves it at /sitemap.xml; the function can fetch URLs from a database or CMS.

Further reading

Problem with a question

Next.js · Junior · question 52

Test yourself

Take the Next.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz