Add environment variable configuration for WEBMENTION_TOKEN and simplify token handling in fetchMentions function
All checks were successful
Deploy / deploy (push) Successful in 1m18s

This commit is contained in:
Adrian Altner 2026-04-22 02:44:21 +02:00
parent 1a3e4bf64a
commit d7cb6b5346
2 changed files with 16 additions and 11 deletions

View file

@ -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,
},

View file

@ -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<FetchResult> {
// Bracket notation prevents Vite from statically replacing at build time.
const envKey = 'WEBMENTION_TOKEN';
const processToken =
typeof process !== 'undefined' ? (process.env as Record<string, string | undefined>)[envKey] : undefined;
const importMetaToken = (import.meta.env as Record<string, string | undefined>)[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<FetchResult> {
}
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}`,
};
}