Skip to content

Commit

Permalink
fix: broken header merging breaking fetch
Browse files Browse the repository at this point in the history
Fixes #400
  • Loading branch information
harlan-zw committed Jan 16, 2025
1 parent c37be55 commit e943b86
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/runtime/server/sitemap/builder/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export async function buildSitemapUrls(sitemap: SitemapDefinition, resolvers: Ni
// always fetch all sitemap data for the primary sitemap
const sourcesInput = sitemap.includeAppSources ? await globalSitemapSources() : []
sourcesInput.push(...await childSitemapSources(sitemap))
const sources = await resolveSitemapSources(sourcesInput, resolvers.event)
const sources = await resolveSitemapSources(sourcesInput)
const resolvedCtx: SitemapInputCtx = {
urls: sources.flatMap(s => s.urls),
sitemapName: sitemap.sitemapName,
Expand Down
23 changes: 11 additions & 12 deletions src/runtime/server/sitemap/urlset/sources.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { getRequestHost } from 'h3'
import type { H3Event } from 'h3'
import type { FetchError } from 'ofetch'
import { defu } from 'defu'
import type {
ModuleRuntimeConfig,
SitemapSourceBase,
SitemapSourceResolved,
SitemapUrlInput,
} from '../../../types'

export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceResolved, event?: H3Event): Promise<SitemapSourceResolved> {
export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceResolved): 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 @@ -23,14 +20,16 @@ export async function fetchDataSource(input: SitemapSourceBase | SitemapSourceRe

let isHtmlResponse = false
try {
const fetchContainer = (url.startsWith('/') && event) ? event : globalThis
const urls = await fetchContainer.$fetch(url, {
const urls = await globalThis.$fetch(url, {
...options,
responseType: 'json',
signal: timeoutController.signal,
headers: defu(options?.headers, {
Accept: 'application/json',
}, event ? { Host: getRequestHost(event, { xForwardedHost: true }) } : {}),
headers: {
...(options?.headers || {}),
...{
Accept: 'application/json',
},
},
// @ts-expect-error untyped
onResponse({ response }) {
if (typeof response._data === 'string' && response._data.startsWith('<!DOCTYPE html>'))
Expand Down Expand Up @@ -86,12 +85,12 @@ export function childSitemapSources(definition: ModuleRuntimeConfig['sitemaps'][
return (
definition?._hasSourceChunk
? import(`#sitemap-virtual/child-sources.mjs`)
.then(m => m.sources[definition.sitemapName] || [])
.then(m => m.sources[definition.sitemapName] || [])
: Promise.resolve([])
)
}

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

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

0 comments on commit e943b86

Please sign in to comment.