To generate multiple sitemaps in Next.js 15, you have two main options:
- Nesting sitemap files: Place
sitemap.(js|ts|xml) files in different route segments, e.g., app/sitemap.xml, app/blog/sitemap.xml, etc.
- Using
generateSitemaps: Use the generateSitemaps function to split your sitemap into multiple files, which is especially useful for large sites. This will generate URLs like /sitemap/0.xml, /sitemap/1.xml, etc. or /blog/sitemap/0.xml for nested sitemaps.
Main Sitemap Index
If you want mywebsite.com/sitemap.xml to act as a sitemap index (listing links to all other sitemaps), you need to return the correct structure. However, the built-in Next.js sitemap API is designed to generate sitemaps of URLs, not sitemap indexes (lists of other sitemaps). The documentation does not currently describe a way to generate a sitemap index file natively using the sitemap.ts convention. This is a known limitation and has been raised as an issue by the community [Using sitemap.ts with generateSitemaps() does not generate a root sitemap.xml].
Why /sitemap-blog.xml Returns an Error
If you want to serve a sitemap at /sitemap-blog.xml, you need to create a file at app/sitemap-blog.xml/route.ts (or .js/.ts as appropriate). The code you posted only creates a sitemap at /sitemap.xml and links to /sitemap-blog.xml, but unless you actually implement a route for /sitemap-blog.xml, it will 404.
Example Structure
app/
sitemap.xml/ # Main sitemap or sitemap index
route.ts
sitemap-blog.xml/ # Blog sitemap
route.ts
sitemap-products.xml/ # Products sitemap
route.ts
Each route.ts should export a function that returns the correct sitemap structure.
Example: Main Sitemap Index (Manual Approach)