From 4f67fb99d415ed0e79fba5d85020d076ff556601 Mon Sep 17 00:00:00 2001 From: Adrian Altner Date: Wed, 22 Apr 2026 02:25:37 +0200 Subject: [PATCH] Refactor fetchMentions function to return structured results with debug information --- src/components/Webmentions.astro | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/components/Webmentions.astro b/src/components/Webmentions.astro index 38732f5..82cbd24 100644 --- a/src/components/Webmentions.astro +++ b/src/components/Webmentions.astro @@ -24,9 +24,15 @@ interface Props { const { target, locale = getLocaleFromUrl(Astro.url) ?? DEFAULT_LOCALE } = Astro.props; -async function fetchMentions(target: string): Promise { +interface FetchResult { + mentions: WMEntry[]; + debug: string; +} + +async function fetchMentions(target: string): Promise { const token = import.meta.env.WEBMENTION_TOKEN; - if (!token) return []; + const tokenLen = typeof token === 'string' ? token.length : 0; + if (!token) return { mentions: [], debug: `no-token(len=${tokenLen})` }; const withSlash = target.endsWith('/') ? target : `${target}/`; const withoutSlash = target.replace(/\/+$/, ''); const fetchOne = async (t: string) => { @@ -35,24 +41,27 @@ async function fetchMentions(target: string): Promise { url.searchParams.set('token', token); url.searchParams.set('per-page', '100'); const res = await fetch(url); - if (!res.ok) return [] as WMEntry[]; + if (!res.ok) return { entries: [] as WMEntry[], status: res.status }; const json = (await res.json()) as { children?: WMEntry[] }; - return json.children ?? []; + return { entries: json.children ?? [], status: 200 }; }; const [a, b] = await Promise.all([fetchOne(withSlash), fetchOne(withoutSlash)]); const seen = new Set(); const merged: WMEntry[] = []; - for (const m of [...a, ...b]) { + for (const m of [...a.entries, ...b.entries]) { const id = m['wm-id']; if (id == null || seen.has(id)) continue; seen.add(id); merged.push(m); } - return merged; + return { + mentions: merged, + debug: `len=${tokenLen} slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`, + }; } const targetStr = target.toString(); -const all = await fetchMentions(targetStr); +const { mentions: all, debug: fetchDebug } = await fetchMentions(targetStr); const likes = all.filter((m) => m['wm-property'] === 'like-of'); const reposts = all.filter((m) => m['wm-property'] === 'repost-of'); @@ -79,7 +88,7 @@ function formatDate(iso?: string) { const hasAny = facepile.length > 0 || replies.length > 0 || mentions.length > 0; --- - + { hasAny && (