FAQ // All answers

How do I create a sitemap in Next.js?

Next.js supports XML sitemaps natively via a sitemap.ts file in the app directory (App Router) or a getServerSideProps route in pages/ (Pages Router). Create app/sitemap.ts exporting a default function that returns an array of { url, lastModified, changeFrequency, priority } objects — Next.js serves it at /sitemap.xml with the correct Content-Type. For dynamic routes (blog posts, products, docs), fetch the slug list inside sitemap.ts and emit one entry per item. For sites above 50,000 URLs, generate a sitemap-index plus multiple child sitemaps with the same pattern (Next.js 13.4+ supports splitting). Avoid the legacy approach of writing static files to /public — it goes stale fast and doesn't follow dynamic data.

RELATED

Keep reading.