Multi Sitemaps
Introduction
By default, the module generates a single /sitemap.xml
file, which works perfectly for most websites.
For larger sites with thousands of URLs, multiple sitemaps offer several benefits:
- Easier debugging and management
- More efficient search engine crawling
- Better organization of content types
Enabling Multiple Sitemaps
You can enable multiple sitemaps using the sitemaps
option in two ways:
- Manual Chunking (
object
): Best for sites with clear content types (pages, posts, etc) or fewer than 1000 URLs - Automatic Chunking (
true
): Best for sites with more than 1000 URLs without clear content types
export default defineNuxtConfig({
sitemap: {
// manually chunk into multiple sitemaps
sitemaps: {
posts: {
include: [
'/blog/**',
],
// example: give blog posts slightly higher priority (this is optional)
defaults: { priority: 0.7 },
},
pages: {
exclude: [
'/blog/**',
]
},
},
},
})
export default defineNuxtConfig({
sitemap: {
sitemaps: true,
// modify the chunk size if you need
defaultSitemapsChunkSize: 2000 // default 1000
},
})
Customizing Sitemap URLs
By default, all multi-sitemaps are served under the /__sitemap__/
prefix. You can customize this behavior to create cleaner URLs:
export default defineNuxtConfig({
sitemap: {
sitemapsPathPrefix: '/', // or false
sitemaps: {
// will be available at /sitemap-foo.xml
['sitemap-foo']: {
// ...
}
}
}
})
Manual Chunking
Manual chunking gives you complete control over how your URLs are distributed across sitemaps. This approach is ideal when you have distinct content types or specific organizational needs.
Setting Default Values
You can provide default values for URLs within each sitemap using the defaults
option:
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts: {
// posts low priority
defaults: { priority: 0.7 },
},
},
},
})
Extending App Sources
When you already have all URLs in your single sitemap but want to split them into separate sitemaps, you can extend existing app sources and apply filters.
Available options:
includeAppSources
: Include URLs from automatic app sourcesincludeGlobalSources
: Include URLs from global sourcesinclude
: Array of glob patterns to includeexclude
: Array of glob patterns to exclude
export default defineNuxtConfig({
sitemap: {
sitemaps: {
pages: {
// extend the nuxt:pages app source
includeAppSources: true,
// filter the URLs to only include pages
exclude: ['/blog/**'],
},
posts: {
// extend the nuxt:pages app source
includeAppSources: true,
// filter the URLs to only include pages
include: ['/blog/**'],
},
},
},
})
Using the _sitemap
Key
When using global sources and need to direct specific URLs to particular sitemaps, use the _sitemap
key:
export default defineNuxtConfig({
sitemap: {
sources: [
'/api/sitemap-urls'
],
sitemaps: {
pages: {
includeGlobalSources: true,
includeAppSources: true,
exclude: ['/**']
// ...
},
},
},
})
export default defineSitemapEventHandler(() => {
return [
{
loc: '/about-us',
// will end up in the pages sitemap
_sitemap: 'pages',
}
]
})
Managing Custom Sources
For sitemaps that need to fetch URLs from endpoints, you have two options:
urls
: Static URLs to include in the sitemap (avoid for large URL sets)sources
: Endpoints to fetch dynamic URLs from (JSON or XML)
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts: {
urls() {
// resolved when the sitemap is shown
return ['/foo', '/bar']
},
sources: [
'/api/sitemap-urls'
]
},
},
},
})
Chunking Large Sources
When you have sources that return a large number of URLs, you can enable chunking to split them into multiple XML files:
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts: {
sources: ['/api/posts'], // returns 10,000 posts
chunks: true, // Enable chunking with default size (1000)
},
products: {
sources: ['/api/products'], // returns 50,000 products
chunks: 5000, // Chunk into files with 5000 URLs each
},
articles: {
sources: ['/api/articles'],
chunks: true,
chunkSize: 2000, // Alternative way to specify chunk size
}
}
},
})
This will generate:
/sitemap_index.xml
- Lists all sitemaps including chunks/posts-0.xml
- First 1000 posts/posts-1.xml
- Next 1000 posts/products-0.xml
- First 5000 products/products-1.xml
- Next 5000 products- etc.
Linking External Sitemaps
Use the special index
key to add external sitemaps to your sitemap index:
export default defineNuxtConfig({
sitemaps: {
// generated sitemaps
posts: {
// ...
},
pages: {
// ...
},
// extending the index sitemap with an external sitemap
index: [
{ sitemap: 'https://www.google.com/sitemap-pages.xml' }
]
}
})
Automatic Chunking
Automatic chunking divides your sitemap into multiple files based on URL count. This feature:
- Uses numbered naming convention (
0-sitemap.xml
,1-sitemap.xml
, etc.) - Chunks based on
defaultSitemapsChunkSize
(default: 1000 URLs per sitemap) - Should be avoided for sites with fewer than 1000 URLs
export default defineNuxtConfig({
sitemap: {
// automatically chunk into multiple sitemaps
sitemaps: true,
// optionally customize chunk size
defaultSitemapsChunkSize: 2000 // default: 1000
},
})