Deviopath › Next.js

sitemap.ts

Easy · SEO

A file app/sitemap.ts can default-export a function that returns a list of { url, lastModified } entries. Next.js serves it as /sitemap.xml, so search engines can find all your pages.

Write sitemap(posts, baseUrl, today) that returns:

- first the home page: { url: baseUrl, lastModified: today }

- then one entry per post: { url: baseUrl + "/blog/" + slug, lastModified: post.updatedAt }, in the same order

Simplified: the real function takes no arguments; it fetches the posts itself and would use new Date() instead of today.

Next: Config Redirects