From 0614688d2efff60c0563e04edf49c4071d525c87 Mon Sep 17 00:00:00 2001 From: Adrian Altner Date: Wed, 22 Apr 2026 02:47:25 +0200 Subject: [PATCH] Refactor fetchMentions function to enhance token retrieval and debug output --- src/components/Webmentions.astro | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/components/Webmentions.astro b/src/components/Webmentions.astro index 03ba39e..cf3c08a 100644 --- a/src/components/Webmentions.astro +++ b/src/components/Webmentions.astro @@ -31,9 +31,19 @@ interface FetchResult { } async function fetchMentions(target: string): Promise { - const token = WEBMENTION_TOKEN; + // Probe multiple env sources to see which one has the token at build time. + const fromAstroEnv = WEBMENTION_TOKEN; + const procEnv = typeof process !== 'undefined' ? process.env : {}; + const procKeyCount = Object.keys(procEnv).length; + const hasWebmentionKey = 'WEBMENTION_TOKEN' in procEnv; + const procValLen = + hasWebmentionKey && typeof procEnv['WEBMENTION_TOKEN'] === 'string' + ? (procEnv['WEBMENTION_TOKEN'] as string).length + : 0; + const token = fromAstroEnv || (hasWebmentionKey ? (procEnv['WEBMENTION_TOKEN'] as string) : undefined); const tokenLen = typeof token === 'string' ? token.length : 0; - if (!token) return { mentions: [], debug: `no-token(astroenv=${tokenLen})` }; + const probe = `ae=${fromAstroEnv ? (fromAstroEnv as string).length : 0},pk=${procKeyCount},phas=${hasWebmentionKey},pv=${procValLen}`; + if (!token) return { mentions: [], debug: `no-token(${probe})` }; const withSlash = target.endsWith('/') ? target : `${target}/`; const withoutSlash = target.replace(/\/+$/, ''); const fetchOne = async (t: string) => { @@ -57,7 +67,7 @@ async function fetchMentions(target: string): Promise { } return { mentions: merged, - debug: `ok(len=${tokenLen}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`, + debug: `ok(${probe}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`, }; }