diff --git a/src/module.ts b/src/module.ts index 3b7b4b8e..150568e1 100644 --- a/src/module.ts +++ b/src/module.ts @@ -88,6 +88,14 @@ export interface ModuleOptions extends SitemapRoot { * @default true */ discoverImages: boolean + /** + * When chunking the sitemaps into multiple files, how many entries should each file contain. + * + * Set to `false` to disabling chunking completely. + * + * @default 1000 + */ + defaultSitemapsChunkSize: number | false /** * Modify the cache behavior. * @@ -173,6 +181,7 @@ export default defineNuxtModule({ credits: true, cacheTtl: 1000 * 60 * 60, // cache for 60 minutes debug: false, + defaultSitemapsChunkSize: 1000, autoLastmod: true, inferStaticPagesAsRoutes: true, discoverImages: true, @@ -360,6 +369,7 @@ declare module 'nitropack/dist/runtime/types' { xsl: config.xsl, xslTips: config.xslTips, cacheTtl: config.cacheTtl, + defaultSitemapsChunkSize: config.defaultSitemapsChunkSize, // @ts-expect-error runtime types runtimeCacheStorage: typeof config.runtimeCacheStorage === 'boolean' ? 'default' : config.runtimeCacheStorage.driver, autoAlternativeLangPrefixes: config.autoAlternativeLangPrefixes, diff --git a/src/runtime/sitemap/builder/sitemap-index.ts b/src/runtime/sitemap/builder/sitemap-index.ts index f3405cfc..5e5cd401 100644 --- a/src/runtime/sitemap/builder/sitemap-index.ts +++ b/src/runtime/sitemap/builder/sitemap-index.ts @@ -6,7 +6,6 @@ import type { SitemapRoot, } from '../../types' import { normaliseDate, normaliseSitemapData, resolveAsyncDataSources } from '../entries' -import { MaxSitemapSize } from '../const' import { escapeValueForXml, wrapSitemapXml } from './util' export async function buildSitemapIndex(options: BuildSitemapIndexInput) { @@ -21,7 +20,7 @@ export async function buildSitemapIndex(options: BuildSitemapIndexInput) { const urls = await normaliseSitemapData(rawEntries.map(e => e.urls).flat(), options) // split into the max size which should be 1000 urls.forEach((url, i) => { - const chunkIndex = Math.floor(i / MaxSitemapSize) + const chunkIndex = Math.floor(i / (options.moduleConfig.defaultSitemapsChunkSize as number)) chunks[chunkIndex] = chunks[chunkIndex] || { urls: [] } chunks[chunkIndex].urls.push(url) }) diff --git a/src/runtime/sitemap/builder/sitemap.ts b/src/runtime/sitemap/builder/sitemap.ts index 023069e4..39c3f589 100644 --- a/src/runtime/sitemap/builder/sitemap.ts +++ b/src/runtime/sitemap/builder/sitemap.ts @@ -1,6 +1,5 @@ import type { BuildSitemapInput, SitemapRenderCtx } from '../../types' import { normaliseSitemapData, resolveAsyncDataSources } from '../entries' -import { MaxSitemapSize } from '../const' import { escapeValueForXml, wrapSitemapXml } from './util' export async function buildSitemap(options: BuildSitemapInput) { @@ -10,8 +9,8 @@ export async function buildSitemap(options: BuildSitemapInput) { // dedupes data let entries = await normaliseSitemapData(sources.map(e => e.urls).flat(), options) // if we're rendering a partial sitemap, slice the entries - if (sitemapsConfig === true) - entries = entries.slice(Number(options.sitemap?.sitemapName) * MaxSitemapSize, (Number(options.sitemap?.sitemapName) + 1) * MaxSitemapSize) + if (sitemapsConfig === true && options.moduleConfig.defaultSitemapsChunkSize) + entries = entries.slice(Number(options.sitemap?.sitemapName) * options.moduleConfig.defaultSitemapsChunkSize, (Number(options.sitemap?.sitemapName) + 1) * options.moduleConfig.defaultSitemapsChunkSize) const ctx: SitemapRenderCtx = { urls: entries, sitemapName: options?.sitemap?.sitemapName || 'sitemap' } await options.callHook?.(ctx) diff --git a/src/runtime/sitemap/const.ts b/src/runtime/sitemap/const.ts deleted file mode 100644 index b7b503b4..00000000 --- a/src/runtime/sitemap/const.ts +++ /dev/null @@ -1 +0,0 @@ -export const MaxSitemapSize = 1000 diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 54896f2b..ed71c661 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -24,7 +24,7 @@ export interface DataSourceResult { timeTakenMs?: number } -export type RuntimeModuleOptions = { urls: SitemapEntryInput[] } & Pick +export type RuntimeModuleOptions = { urls: SitemapEntryInput[] } & Pick export interface ModuleRuntimeConfig { moduleConfig: RuntimeModuleOptions; buildTimeMeta: ModuleComputedOptions }