diff --git a/astro.config.mjs b/astro.config.mjs index d0c8b4b..25fb9a9 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -2,7 +2,7 @@ import mdx from '@astrojs/mdx'; import sitemap from '@astrojs/sitemap'; -import { defineConfig, fontProviders } from 'astro/config'; +import { defineConfig, envField, fontProviders } from 'astro/config'; import node from '@astrojs/node'; @@ -10,6 +10,16 @@ import node from '@astrojs/node'; export default defineConfig({ site: 'https://adrian-altner.de', + env: { + schema: { + WEBMENTION_TOKEN: envField.string({ + context: 'server', + access: 'secret', + optional: true, + }), + }, + }, + devToolbar: { enabled: false, }, diff --git a/src/components/Webmentions.astro b/src/components/Webmentions.astro index 14b5e1f..03ba39e 100644 --- a/src/components/Webmentions.astro +++ b/src/components/Webmentions.astro @@ -1,4 +1,5 @@ --- +import { WEBMENTION_TOKEN } from 'astro:env/server'; import { DEFAULT_LOCALE, type Locale } from '~/consts'; import { getLocaleFromUrl, t } from '~/i18n/ui'; @@ -30,15 +31,9 @@ interface FetchResult { } async function fetchMentions(target: string): Promise { - // Bracket notation prevents Vite from statically replacing at build time. - const envKey = 'WEBMENTION_TOKEN'; - const processToken = - typeof process !== 'undefined' ? (process.env as Record)[envKey] : undefined; - const importMetaToken = (import.meta.env as Record)[envKey]; - const token = processToken || importMetaToken; - const pLen = typeof processToken === 'string' ? processToken.length : 0; - const iLen = typeof importMetaToken === 'string' ? importMetaToken.length : 0; - if (!token) return { mentions: [], debug: `no-token(pe=${pLen},iml=${iLen})` }; + const token = WEBMENTION_TOKEN; + const tokenLen = typeof token === 'string' ? token.length : 0; + if (!token) return { mentions: [], debug: `no-token(astroenv=${tokenLen})` }; const withSlash = target.endsWith('/') ? target : `${target}/`; const withoutSlash = target.replace(/\/+$/, ''); const fetchOne = async (t: string) => { @@ -62,7 +57,7 @@ async function fetchMentions(target: string): Promise { } return { mentions: merged, - debug: `iml=${iLen} pe=${pLen} 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}`, }; }