UNPKG

nuxt-simple-sitemap

Version:

Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.

83 lines (82 loc) 2.82 kB
export async function fetchDataSource(input) { 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]; const options = typeof input.fetch === "string" ? {} : input.fetch[1]; const start = Date.now(); const timeout = options.timeout || 5e3; const timeoutController = new AbortController(); const abortRequestTimeout = setTimeout(() => timeoutController.abort(), timeout); let isHtmlResponse = false; try { const urls = await globalThis.$fetch(url, { responseType: "json", signal: timeoutController.signal, headers: { Accept: "application/json" }, // @ts-expect-error untyped onResponse({ response }) { if (typeof response._data === "string" && response._data.startsWith("<!DOCTYPE html>")) isHtmlResponse = true; } }); const timeTakenMs = Date.now() - start; if (isHtmlResponse) { context.tips.push("This is usually because the URL isn't correct or is throwing an error. Please check the URL"); return { ...input, context, urls: [], timeTakenMs, error: "Received HTML response instead of JSON" }; } return { ...input, context, timeTakenMs, urls }; } catch (_err) { const error = _err; if (error.message.includes("This operation was aborted")) context.tips.push("The request has taken too long. Make sure app sources respond within 5 seconds or adjust the timeout fetch option."); else context.tips.push(`Response returned a status of ${error.response?.status || "unknown"}.`); console.error("[nuxt-simple-sitemap] Failed to fetch source.", { url, error }); return { ...input, context, urls: [], error: error.message }; } finally { abortRequestTimeout && clearTimeout(abortRequestTimeout); } } export function globalSitemapSources() { return import("#nuxt-simple-sitemap/global-sources.mjs").then((m) => m.sources); } export function childSitemapSources(definition) { return definition?._hasSourceChunk ? import("#nuxt-simple-sitemap/child-sources.mjs").then((m) => m.sources[definition.sitemapName] || []) : Promise.resolve([]); } export async function resolveSitemapSources(sources) { return (await Promise.all( sources.map((source) => { if (typeof source === "object" && "urls" in source) { return { timeTakenMs: 0, ...source, urls: source.urls }; } if (source.fetch) return fetchDataSource(source); return { ...source, error: "Invalid source" }; }) )).flat(); }