Multi Sitemaps
Introduction
The module will generate a single /sitemap.xml
file by default, for most websites this is perfect.
However, for larger sites that start having over thousands of URLs, introducing multiple sitemaps can help you to debug your sitemap easier and also help search engines to crawl your site more efficiently.
Enabling Multiple Sitemaps
If you want to generate multiple sitemaps, you can use the sitemaps
option, which has two options:
object
- Enables manual chunking. Recommended when you have clear content types (pages, posts, etc) or less than 1000 URLstrue
- Enables automatic chunking. Recommended when you have a more than 1000 URLs and don't have 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
},
})
Sitemap Prefix
You'll notice that all multi-sitemaps appear under the /__sitemap__/
prefix by default. If you want to change this, you can use the sitemapsPathPrefix
option
combined with changing the sitemap key to what you'd like the name to be.
export default defineNuxtConfig({
sitemap: {
sitemapsPathPrefix: '/', // or false
sitemaps: {
// will be available at /sitemap-foo.xml
['sitemap-foo']: {
// ...
}
}
}
})
Manual Chunking
When manually chunking your sitemaps, there are multiple ways of handling it depending on what you need.
In either case, if you'd like to provide defaults for URLs within the sitemap you can use the defaults
option.
defaults
- Sitemap default values such aslastmod
,changefreq
, orpriority
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts: {
// posts low priority
defaults: { priority: 0.7 },
},
},
},
})
Extending App Sources
When your single sitemap contains all the correct URLs and you just want to split them up into separate sitemaps, you can extend the app sources and filter the URLs.
includeAppSources
- Uses app sourcesincludeGlobalSources
- Uses global sourcesinclude
- Array of glob patterns to include in the sitemapexclude
- Array of glob patterns to exclude from the sitemap
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/**'],
},
},
},
})
If you're using a global sitemap.sources
and need to filter URLs further, then you can use the _sitemap
key.
_sitemap
- The name of the sitemap that the URL should be included in
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 Sources
If you need to fetch the URLs from an endpoint for a sitemap, then you will need to use either the urls
or sources
option.
urls
- Array of static URLs to include in the sitemap. You should avoid using this option if you have a lot of URLssources
- Custom endpoint to fetch dynamic URLs from as JSON or XML.
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts: {
urls() {
// resolved when the sitemap is shown
return ['/foo', '/bar']
},
sources: [
'/api/sitemap-urls'
]
},
},
},
})
Linking External Sitemaps
This mode also provides a special key called index
which allows you to easily extend the index sitemap. This can be useful
for adding an external sitemap.
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
This will automatically chunk your sitemap into multiple-sitemaps, using the 0-sitemap.xml
, 1-sitemap.xml
naming convention.
It will be chunked on the defaultSitemapsChunkSize
option, which defaults to 1000 URLs per sitemap.
You should avoid using this if you have less than 1000 URLs.
export default defineNuxtConfig({
sitemap: {
// automatically chunk into multiple sitemaps
sitemaps: true,
},
})