diff --git a/Containerfile b/Containerfile index 2e1b833..06e85f9 100644 --- a/Containerfile +++ b/Containerfile @@ -11,9 +11,6 @@ RUN npm ci COPY . . -RUN printf '%s' "${WEBMENTION_TOKEN}" > /app/.webmention-token -RUN echo "TOKEN FILE:" && ls -la /app/.webmention-token && wc -c /app/.webmention-token - RUN npm run build diff --git a/astro.config.mjs b/astro.config.mjs index d0c8b4b..306244d 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -3,13 +3,25 @@ import mdx from '@astrojs/mdx'; import sitemap from '@astrojs/sitemap'; import { defineConfig, fontProviders } from 'astro/config'; +import { loadEnv } from 'vite'; import node from '@astrojs/node'; +const envMode = process.env.NODE_ENV === 'production' ? 'production' : 'development'; +const envVars = loadEnv(envMode, process.cwd(), ''); +const WEBMENTION_TOKEN = envVars.WEBMENTION_TOKEN || process.env.WEBMENTION_TOKEN || ''; +console.log(`[astro.config] loadEnv mode=${envMode} token-len=${WEBMENTION_TOKEN.length}`); + // https://astro.build/config export default defineConfig({ site: 'https://adrian-altner.de', + vite: { + define: { + 'globalThis.__WEBMENTION_TOKEN__': JSON.stringify(WEBMENTION_TOKEN), + }, + }, + devToolbar: { enabled: false, }, diff --git a/src/components/Webmentions.astro b/src/components/Webmentions.astro index 4dc0272..1224745 100644 --- a/src/components/Webmentions.astro +++ b/src/components/Webmentions.astro @@ -1,32 +1,13 @@ --- -import { readFileSync } from 'node:fs'; import { DEFAULT_LOCALE, type Locale } from '~/consts'; import { getLocaleFromUrl, t } from '~/i18n/ui'; -let fileDebug = ''; -function readTokenFromFile(): string | undefined { - const paths = ['/app/.webmention-token', '.webmention-token']; - const logs: string[] = []; - logs.push(`cwd=${process.cwd()}`); - for (const p of paths) { - try { - const raw = readFileSync(p, 'utf-8'); - logs.push(`${p}:ok(${raw.length})`); - const t = raw.trim(); - if (t) { - fileDebug = logs.join(';'); - return t; - } - } catch (err) { - logs.push(`${p}:err(${(err as Error).code ?? 'unknown'})`); - } - } - fileDebug = logs.join(';'); - return undefined; +declare global { + // eslint-disable-next-line no-var + var __WEBMENTION_TOKEN__: string; } - -const FILE_TOKEN = readTokenFromFile(); -const WEBMENTION_TOKEN = FILE_TOKEN ?? (import.meta.env as Record).WEBMENTION_TOKEN; +const tokenRaw = (globalThis as unknown as { __WEBMENTION_TOKEN__?: string }).__WEBMENTION_TOKEN__; +const WEBMENTION_TOKEN = typeof tokenRaw === 'string' ? tokenRaw : ''; interface WMAuthor { name?: string; @@ -58,8 +39,7 @@ interface FetchResult { async function fetchMentions(target: string): Promise { const token = WEBMENTION_TOKEN; const tokenLen = typeof token === 'string' ? token.length : 0; - const source = FILE_TOKEN ? 'file' : 'env'; - if (!token) return { mentions: [], debug: `no-token(src=${source},len=${tokenLen},probe=${fileDebug})` }; + if (!token) return { mentions: [], debug: `no-token(len=${tokenLen})` }; const withSlash = target.endsWith('/') ? target : `${target}/`; const withoutSlash = target.replace(/\/+$/, ''); const fetchOne = async (t: string) => { @@ -83,7 +63,7 @@ async function fetchMentions(target: string): Promise { } return { mentions: merged, - debug: `ok(src=${source},len=${tokenLen},probe=${fileDebug}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`, + debug: `ok(len=${tokenLen}) slash=${a.status}:${a.entries.length} noslash=${b.status}:${b.entries.length}`, }; }