Skip to content

Commit

Permalink
feat: nitro hook sitemap:index-resolved
Browse files Browse the repository at this point in the history
Relates to #237
  • Loading branch information
harlan-zw committed May 22, 2024
1 parent 15b3d6d commit 19094b6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .playground/server/plugins/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export default defineNitroPlugin((nitroApp) => {
// eslint-disable-next-line no-console
console.log('Sitemap SSR hook')
})
nitroApp.hooks.hook('sitemap:index-resolved', (ctx) => {
// eslint-disable-next-line no-console
console.log('Sitemap index resolved hook', ctx)
})
})
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ declare module 'nitropack' {
sitemap?: import('${typesPath}').SitemapItemDefaults
}
interface NitroRuntimeHooks {
'sitemap:index-resolved': (ctx: import('${typesPath}').SitemapRenderCtx) => void | Promise<void>
'sitemap:resolved': (ctx: import('${typesPath}').SitemapRenderCtx) => void | Promise<void>
'sitemap:output': (ctx: import('${typesPath}').SitemapOutputHookCtx) => void | Promise<void>
}
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/nitro/sitemap/builder/sitemap-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { applyI18nEnhancements } from '../urlset/i18n'
import { filterSitemapUrls } from '../urlset/filter'
import { sortSitemapUrls } from '../urlset/sort'
import { escapeValueForXml, wrapSitemapXml } from './xml'
import { useNitroApp } from '#imports'

export async function buildSitemapIndex(resolvers: NitroUrlResolvers, runtimeConfig: ModuleRuntimeConfig) {
const {
Expand Down Expand Up @@ -109,7 +110,11 @@ export async function buildSitemapIndex(resolvers: NitroUrlResolvers, runtimeCon
}))
}

const sitemapXml = entries.map(e => [
const ctx = { sitemaps: entries }
const nitro = useNitroApp()
await nitro.hooks.callHook('sitemap:index-resolved', ctx)

const sitemapXml = ctx.sitemaps.map(e => [
' <sitemap>',
` <loc>${escapeValueForXml(e.sitemap)}</loc>`,
// lastmod is optional
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ export interface SitemapDefinition {
_route?: string
}

export interface SitemapIndexRenderCtx {
sitemaps: SitemapIndexEntry[]
}

export interface SitemapRenderCtx {
sitemapName: string
urls: ResolvedSitemapUrl[]
Expand Down

0 comments on commit 19094b6

Please sign in to comment.