Skip to content

Commit

Permalink
fix: support experimentalWarmUp
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 13, 2023
1 parent c7f4ce0 commit da07f0d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ export interface ModuleOptions extends SitemapDefinition {
* @default true
*/
sortEntries: boolean
/**
* Warm up the sitemap route(s) cache when Nitro starts.
*
* May be implemented by default in a future minor version.
*
* @experimental
*/
experimentalWarmUp?: boolean
}

export interface ModuleHooks {
Expand Down Expand Up @@ -396,6 +404,9 @@ declare module 'vue-router' {
nuxt.options.routeRules[`/${config.sitemapName}`] = routeRules
}

if (config.experimentalWarmUp)
addServerPlugin(resolve('./runtime/plugins/warm-up'))

// @ts-expect-error runtime types
if (hasNuxtModule('@nuxt/content') && (!!nuxt.options.content?.documentDriven || config.strictNuxtContentPaths)) {
addServerPlugin(resolve('./runtime/plugins/nuxt-content'))
Expand Down Expand Up @@ -452,6 +463,7 @@ declare module 'vue-router' {
})
sitemaps.index = {
sitemapName: 'index',
_route: withBase('sitemap_index.xml', nuxt.options.app.baseURL || '/'),
// TODO better index support
// @ts-expect-error untyped
sitemaps: config.sitemaps!.index || [],
Expand All @@ -464,6 +476,7 @@ declare module 'vue-router' {
sitemaps[sitemapName as keyof typeof sitemaps] = defu(
{
sitemapName,
_route: withBase(`${sitemapName}-sitemap.xml`, nuxt.options.app.baseURL || '/'),
_hasSourceChunk: typeof definition.urls !== 'undefined' || definition.sources?.length || !!definition.dynamicUrlsApiEndpoint,
},
{ ...definition, urls: undefined, sources: undefined },
Expand All @@ -485,6 +498,7 @@ declare module 'vue-router' {
// note: we don't need urls for the root sitemap, only child sitemaps
sitemaps[config.sitemapName] = <SitemapDefinition> {
sitemapName: config.sitemapName,
route: withBase(config.sitemapName, nuxt.options.app.baseURL || '/'), // will contain the xml
defaults: config.defaults,
include: config.include,
exclude: config.exclude,
Expand Down
24 changes: 24 additions & 0 deletions src/runtime/plugins/warm-up.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
import { withLeadingSlash } from 'ufo'
import { useRuntimeConfig } from '#imports'

export default defineNitroPlugin((nitroApp) => {
const { sitemaps } = useRuntimeConfig()['nuxt-simple-sitemap']
const queue: (() => Promise<Response>)[] = []
const sitemapsWithRoutes = Object.entries(sitemaps)
.filter(([, sitemap]) => sitemap._route)
for (const [, sitemap] of sitemapsWithRoutes)
queue.push(() => nitroApp.localFetch(withLeadingSlash(sitemap._route), {}))

// run async
setTimeout(() => {
// work the queue step by step await the promise from each task, delay 1s after each task ends
const next = async () => {
if (queue.length === 0)
return
await queue.shift()!()
setTimeout(next, 1000) // arbitrary delay to avoid throttling
}
next()
}, 2500 /* https://github.com/unjs/nitro/pull/1906 */)
})
6 changes: 5 additions & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface AutoI18nConfig {

export interface ModuleRuntimeConfig extends Pick<ModuleOptions, 'sitemapName' | 'excludeAppSources' | 'sortEntries' | 'defaultSitemapsChunkSize' | 'xslColumns' | 'xslTips' | 'debug' | 'discoverImages' | 'autoLastmod' | 'xsl' | 'credits' > {
version: string
sitemaps: { index?: { sitemapName: string; sitemaps: SitemapIndexEntry[] } } & Record<string, Omit<SitemapDefinition, 'urls' | 'sources'> & { _hasSourceChunk?: boolean }>
sitemaps: { index?: Pick<SitemapDefinition, 'sitemapName' | '_route'> & { sitemaps: SitemapIndexEntry[] } } & Record<string, Omit<SitemapDefinition, 'urls' | 'sources'> & { _hasSourceChunk?: boolean }>
autoI18n?: AutoI18nConfig
isMultiSitemap: boolean
isI18nMapped: boolean
Expand Down Expand Up @@ -102,6 +102,10 @@ export interface SitemapDefinition {
* @deprecated use `sources`
*/
dynamicUrlsApiEndpoint?: string | false
/**
* @internal
*/
_route?: string
}

export interface SitemapRenderCtx {
Expand Down

0 comments on commit da07f0d

Please sign in to comment.