Skip to content

Commit

Permalink
fix: forward host header to dynamic source endpoint (#280)
Browse files Browse the repository at this point in the history
Co-authored-by: Ray Chen <chenyuncai@tmxmall.com>
  • Loading branch information
chenyuncai and chenyuncaiwork committed May 22, 2024
1 parent f1c7657 commit 85456e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/runtime/nitro/sitemap/builder/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function buildSitemap(sitemap: SitemapDefinition, resolvers: NitroU
// always fetch all sitemap data for the primary sitemap
const sources = sitemap.includeAppSources ? await globalSitemapSources() : []
sources.push(...await childSitemapSources(sitemap))
let resolvedSources = await resolveSitemapSources(sources)
let resolvedSources = await resolveSitemapSources(sources, resolvers.event)
// normalise the sources for i18n
if (autoI18n)
resolvedSources = normaliseI18nSources(resolvedSources, { autoI18n, isI18nMapped, ...sitemap })
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/nitro/sitemap/urlset/sources.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type H3Event, getHeader } from 'h3';
import type { FetchError } from 'ofetch'
import { defu } from 'defu'
import type {
Expand All @@ -7,7 +8,7 @@ import type {
SitemapUrlInput,
} from '../../../types'

export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceResolved): Promise<SitemapSourceResolved> {
export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceResolved, event?: H3Event): Promise<SitemapSourceResolved> {
const context = typeof input.context === 'string' ? { name: input.context } : input.context || { name: 'fetch' }
context.tips = context.tips || []
const url = typeof input.fetch === 'string' ? input.fetch : input.fetch![0]
Expand All @@ -21,12 +22,14 @@ export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceRe

let isHtmlResponse = false
try {
const currentRequestHost = event ? getHeader(event, 'host') : ''
const urls = await globalThis.$fetch(url, {
...options,
responseType: 'json',
signal: timeoutController.signal,
headers: defu(options?.headers, {
Accept: 'application/json',
Host: currentRequestHost,
}),
// @ts-expect-error untyped
onResponse({ response }) {
Expand Down Expand Up @@ -88,7 +91,7 @@ export function childSitemapSources(definition: ModuleRuntimeConfig['sitemaps'][
) as Promise<(SitemapSourceBase | SitemapSourceResolved)[]>
}

export async function resolveSitemapSources(sources: (SitemapSourceBase | SitemapSourceResolved)[]) {
export async function resolveSitemapSources(sources: (SitemapSourceBase | SitemapSourceResolved)[], event?: H3Event) {
return (await Promise.all(
sources.map((source) => {
if (typeof source === 'object' && 'urls' in source) {
Expand All @@ -99,7 +102,7 @@ export async function resolveSitemapSources(sources: (SitemapSourceBase | Sitema
}
}
if (source.fetch)
return fetchDataSource(source)
return fetchDataSource(source, event)

return <SitemapSourceResolved> {
...source,
Expand Down

0 comments on commit 85456e2

Please sign in to comment.