#Generate multiple sitemaps

1 messages · Page 1 of 1 (latest)

loud plaza
#

i wanna create many sitemaps for my nextjs app 15 website . i wanted to have the main sitemap which is mywebsite/sitemap.xml , then inside i put all the sitemaps , is it correct? also when i open mywebsite/sitemap-blog.xml it redirects me to error page

import { getHostUrl } from "@/utils/getHostUrl";
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const siteBaseUrl = await getHostUrl();
return [
 {
url: ${siteBaseUrl}/sitemap-blog.xml,
 },
 ];
}```
vast creekBOT
#

Hi @loud plaza. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To generate multiple sitemaps in Next.js 15, you have two main options:

  1. Nesting sitemap files: Place sitemap.(js|ts|xml) files in different route segments, e.g., app/sitemap.xml, app/blog/sitemap.xml, etc.
  2. 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)

#

If you want /sitemap.xml to be a sitemap index, you must manually return XML with <sitemapindex> and <sitemap> entries, as the built-in API does not do this for you.

// app/sitemap.xml/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  const xml = `<?xml version="1.0" encoding="UTF-8"?>
    <sitemapindex xmlns="<http://www.sitemaps.org/schemas/sitemap/0.9">>
      <sitemap>
        <loc><https://mywebsite.com/sitemap-blog.xml</loc>>
      </sitemap>
      <sitemap>
        <loc><https://mywebsite.com/sitemap-products.xml</loc>>
      </sitemap>
    </sitemapindex>`;
  return new NextResponse(xml, {
    headers: { 'Content-Type': 'application/xml' },
  });
}

Then, implement app/sitemap-blog.xml/route.ts and app/sitemap-products.xml/route.ts to return the actual sitemaps for those sections.

Summary

  • Your approach is correct in principle, but you must actually implement the routes for each sitemap you want to link.
  • The built-in Next.js sitemap API does not generate a sitemap index for you; you must do this manually.
  • If /sitemap-blog.xml 404s, it's because you haven't created a route for it.

For more details, see the official docs on generating multiple sitemaps and generateSitemaps. The current limitations and workarounds are also discussed in this GitHub issue.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: