/* eslint-disable @next/next/no-img-element */ import { ImageResponse } from '@vercel/og'; import { NextRequest } from 'next/server'; export const config = { runtime: 'edge', }; const fontRegular = fetch( new URL('../../../assets/Inter-Regular.ttf', import.meta.url) ).then((res) => res.arrayBuffer()); const fontSemiBold = fetch( new URL('../../../assets/Inter-SemiBold.ttf', import.meta.url) ).then((res) => res.arrayBuffer()); const handle = async (req: NextRequest) => { const InterRegular = await fontRegular; const InterSemiBold = await fontSemiBold; const { searchParams, host, protocol } = new URL(req.url); const title = searchParams.get('title') || 'No post title'; const author = searchParams.get('author') || 'Anonymous'; const date = new Date(searchParams.get('date') || '2022-08-22'); const category = searchParams.get('category') || 'doc'; const tagsString = searchParams.get('tags') || ''; const tags = tagsString.split(','); const cover = `${protocol}//${host}/_next/image?url=${encodeURIComponent( searchParams.get('cover') || category === 'doc' ? '/og/og-base.jpg' : '/og/og-cover.png' )}&w=1200&q=75`; const logo = `${protocol}//${host}/_next/image?url=${encodeURIComponent( '/og/og-logo.png' )}&w=128&q=75`; if (category === 'doc') { return new ImageResponse( (
Fluid Design
{title}
{tags.length > 0 && tags[0] !== '' && tags.map((tag) => (
{tag}
))}
{author + ' – ' + date.toLocaleDateString('en-US', { dateStyle: 'long' })}
), { width: 1200, height: 628, fonts: [ { name: 'Inter', data: InterRegular, weight: 400, }, { name: 'Inter', data: InterSemiBold, weight: 600, }, ], } ); } else { return new ImageResponse( (
) ); } }; export default handle;