import { ImageResponse } from 'next/og'; import type { NextRequest } from 'next/server'; import { t } from '@/i18n'; import { api, safe } from '@/lib/api'; import { splitCompareSlugs } from '@/lib/compare-state'; import { OG_INK2, OG_INK3, OG_RULE, OG_SIZE_H, OG_SIZE_W, OgFrame, OgWordmark } from '../../og-shared'; export const revalidate = 3600; /** * Social image for /compare/a/b/c — `GET /compare/og?slugs=canada,united-states,france`. * (A file-convention `opengraph-image` cannot live under a catch-all segment, so the page's metadata points here.) */ export async function GET(req: NextRequest) { const raw = req.nextUrl.searchParams.get('slugs') ?? ''; const list = await safe(api.countries()); const bySlug = new Map((list?.items ?? []).flatMap((c) => [[c.slug ?? '', c] as const, [c.id.toLowerCase(), c] as const])); const wanted = splitCompareSlugs(raw.split('/')); const countries = wanted.map((s) => bySlug.get(s)).filter((c): c is NonNullable => !!c); const names = countries.length ? countries.map((c) => c.name ?? c.id) : wanted.map((s) => s.replace(/-/g, ' ')); const flagSize = countries.length > 5 ? 84 : countries.length > 3 ? 108 : 132; const nameSize = names.join('').length > 40 ? 40 : 56; return new ImageResponse( (
{countries.map((c) => (
{c.flag ?? ''}
))}
{names.map((n, i) => (
{i > 0 ? {t('compare.vs')} : null} {n}
))}
{t('compare.title')} · {t('home.compare.sub')}
{t('og.site')}
), { width: OG_SIZE_W, height: OG_SIZE_H }, ); }