Skip to content

Commit

Permalink
feat: defaultSitemapsChunkSize
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 28, 2023
1 parent add9d89 commit 9ad07fc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -173,6 +181,7 @@ export default defineNuxtModule<ModuleOptions>({
credits: true,
cacheTtl: 1000 * 60 * 60, // cache for 60 minutes
debug: false,
defaultSitemapsChunkSize: 1000,
autoLastmod: true,
inferStaticPagesAsRoutes: true,
discoverImages: true,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/sitemap/builder/sitemap-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
})
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/sitemap/builder/sitemap.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/runtime/sitemap/const.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface DataSourceResult {
timeTakenMs?: number
}

export type RuntimeModuleOptions = { urls: SitemapEntryInput[] } & Pick<ModuleOptions, 'sitemapName' | 'cacheTtl' | 'runtimeCacheStorage' | 'xslColumns' | 'xslTips' | 'debug' | 'discoverImages' | 'autoLastmod' | 'xsl' | 'autoAlternativeLangPrefixes' | 'credits' | 'defaults' | 'include' | 'exclude' | 'sitemaps' | 'dynamicUrlsApiEndpoint'>
export type RuntimeModuleOptions = { urls: SitemapEntryInput[] } & Pick<ModuleOptions, 'defaultSitemapsChunkSize' | 'sitemapName' | 'cacheTtl' | 'runtimeCacheStorage' | 'xslColumns' | 'xslTips' | 'debug' | 'discoverImages' | 'autoLastmod' | 'xsl' | 'autoAlternativeLangPrefixes' | 'credits' | 'defaults' | 'include' | 'exclude' | 'sitemaps' | 'dynamicUrlsApiEndpoint'>

export interface ModuleRuntimeConfig { moduleConfig: RuntimeModuleOptions; buildTimeMeta: ModuleComputedOptions }

Expand Down

0 comments on commit 9ad07fc

Please sign in to comment.