Skip to content

Commit 30b4686

Browse files
committedFeb 18, 2025·
fix: expose event on all nitro hooks
1 parent cf5e582 commit 30b4686

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed
 

‎src/runtime/server/routes/sitemap_index.xml.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { joinURL } from 'ufo'
33
import { useNitroApp } from 'nitropack/runtime'
44
import { useSitemapRuntimeConfig } from '../utils'
55
import { buildSitemapIndex, urlsToIndexXml } from '../sitemap/builder/sitemap-index'
6-
import type { SitemapOutputHookCtx } from '../../types'
6+
import type { SitemapIndexRenderCtx, SitemapOutputHookCtx } from '../../types'
77
import { useNitroUrlResolvers } from '../sitemap/nitro'
88

99
export default defineEventHandler(async (e) => {
@@ -23,11 +23,11 @@ export default defineEventHandler(async (e) => {
2323
)
2424
}
2525

26-
const indexResolvedCtx = { sitemaps }
26+
const indexResolvedCtx: SitemapIndexRenderCtx = { sitemaps, event: e }
2727
await nitro.hooks.callHook('sitemap:index-resolved', indexResolvedCtx)
2828

2929
const output = urlsToIndexXml(indexResolvedCtx.sitemaps, resolvers, runtimeConfig)
30-
const ctx: SitemapOutputHookCtx = { sitemap: output, sitemapName: 'sitemap' }
30+
const ctx: SitemapOutputHookCtx = { sitemap: output, sitemapName: 'sitemap', event: e }
3131
await nitro.hooks.callHook('sitemap:output', ctx)
3232

3333
setHeader(e, 'Content-Type', 'text/xml; charset=UTF-8')

‎src/runtime/server/sitemap/builder/sitemap-index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export async function buildSitemapIndex(resolvers: NitroUrlResolvers, runtimeCon
4343
const resolvedCtx: SitemapInputCtx = {
4444
urls: sources.flatMap(s => s.urls),
4545
sitemapName: sitemap.sitemapName,
46+
event: resolvers.event,
4647
}
4748
await nitro?.hooks.callHook('sitemap:input', resolvedCtx)
4849
const normalisedUrls = resolveSitemapEntries(sitemap, resolvedCtx.urls, { autoI18n, isI18nMapped }, resolvers)

‎src/runtime/server/sitemap/builder/sitemap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export async function buildSitemapUrls(sitemap: SitemapDefinition, resolvers: Ni
228228
const resolvedCtx: SitemapInputCtx = {
229229
urls: sources.flatMap(s => s.urls),
230230
sitemapName: sitemap.sitemapName,
231+
event: resolvers.event,
231232
}
232233
await nitro?.hooks.callHook('sitemap:input', resolvedCtx)
233234
const enhancedUrls = resolveSitemapEntries(sitemap, resolvedCtx.urls, { autoI18n, isI18nMapped }, resolvers)

‎src/runtime/server/sitemap/nitro.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export async function createSitemap(event: H3Event, definition: SitemapDefinitio
9191
const resolvedCtx: SitemapRenderCtx = {
9292
urls: sitemapUrls,
9393
sitemapName: sitemapName,
94+
event,
9495
}
9596
await nitro.hooks.callHook('sitemap:resolved', resolvedCtx)
9697
// we need to normalize any new urls otherwise they won't appear in the final sitemap
@@ -105,7 +106,7 @@ export async function createSitemap(event: H3Event, definition: SitemapDefinitio
105106
const urls = maybeSort(mergeOnKey(normalizedPreDedupe, '_key').map(e => normaliseEntry(e, definition.defaults, resolvers)))
106107
const sitemap = urlsToXml(urls, resolvers, runtimeConfig)
107108

108-
const ctx = { sitemap, sitemapName }
109+
const ctx = { sitemap, sitemapName, event }
109110
await nitro.hooks.callHook('sitemap:output', ctx)
110111
// need to clone the config object to make it writable
111112
setHeader(event, 'Content-Type', 'text/xml; charset=UTF-8')

‎src/runtime/types.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,25 @@ export interface SitemapDefinition {
303303
_route?: string
304304
}
305305

306-
export interface SitemapIndexRenderCtx {
306+
interface NitroBaseHook {
307+
event: H3Event
308+
}
309+
310+
export interface SitemapIndexRenderCtx extends NitroBaseHook {
307311
sitemaps: SitemapIndexEntry[]
308312
}
309313

310-
export interface SitemapRenderCtx {
314+
export interface SitemapRenderCtx extends NitroBaseHook {
311315
sitemapName: string
312316
urls: ResolvedSitemapUrl[]
313317
}
314318

315-
export interface SitemapInputCtx {
319+
export interface SitemapInputCtx extends NitroBaseHook {
316320
sitemapName: string
317321
urls: SitemapUrlInput[]
318322
}
319323

320-
export interface SitemapOutputHookCtx {
324+
export interface SitemapOutputHookCtx extends NitroBaseHook {
321325
sitemapName: string
322326
sitemap: string
323327
}

0 commit comments

Comments
 (0)
Please sign in to comment.