For apps with 100k+ pages, generating a sitemap can be a slow process. As robots will request your sitemap frequently, it's important to keep it fast.
Nuxt SEO provides a default cache engine to keep your sitemaps fast and recommendations on how to improve performance.
When dealing with many URLs that are being generated from an external API, the best option is use the sitemaps
option to create Named Sitemap Chunks.
Each sitemap should contain its own sources, this allows other sitemaps to be generated without waiting for this request.
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts: {
sources: [
'https://api.something.com/urls'
]
},
},
},
})
If you need to split this up further, you should consider chunking by the type and some pagination format. For example, you can paginate by when posts were created.
export default defineNuxtConfig({
sitemap: {
sitemaps: {
posts2020: {
sources: [
'https://api.something.com/urls?filter[yearCreated]=2020'
]
},
posts2021: {
sources: [
'https://api.something.com/urls?filter[yearCreated]=2021'
]
},
},
},
})
Additionally, you may want to consider the following experimental options that may help with performance:
experimentalCompression - Gzip's and streams the sitemapexperimentalWarmUp - Creates the sitemaps when Nitro startsIf your sitemap URLs only change when you deploy (not at runtime), you can enable zeroRuntime to generate sitemaps at build time and eliminate sitemap generation code from your server bundle.
export default defineNuxtConfig({
sitemap: {
zeroRuntime: true
}
})
This reduces server bundle size by ~50KB. The sitemap is generated once at build time and served as a static file.
See the Zero Runtime guide for details.
Caching your sitemap can help reduce the load on your server and improve performance.
By default, SWR caching is enabled on production environments and sitemaps will be cached for 10 minutes.
This is configured by overriding your route rules and leveraging the native Nuxt caching.
You can change the cache time by setting the cacheMaxAgeSeconds option.
export default defineNuxtConfig({
sitemap: {
cacheMaxAgeSeconds: 3600 // 1 hour
}
})
If you want to disable caching, set the cacheMaxAgeSeconds to 0.
The cache engine is set to the Nitro default of the cache/ path.
If you want to customise the cache engine, you can set the runtimeCacheStorage option.
export default defineNuxtConfig({
sitemap: {
// cloudflare kv binding example
runtimeCacheStorage: {
driver: 'cloudflare-kv-binding',
binding: 'OG_IMAGE_CACHE'
}
}
})