import * as React from 'react' import { NextRequest } from 'next/server' import { ImageResponse } from '@vercel/og' import { api, apiHost } from '@/lib/config' import { NotionPageInfo } from '@/lib/types' const interRegularFontP = fetch( new URL('../../public/fonts/Inter-Regular.ttf', import.meta.url) ).then((res) => res.arrayBuffer()) const interBoldFontP = fetch( new URL('../../public/fonts/Inter-SemiBold.ttf', import.meta.url) ).then((res) => res.arrayBuffer()) export const config = { runtime: 'experimental-edge' } export default async function OGImage(req: NextRequest) { const [interRegularFont, interBoldFont] = await Promise.all([ interRegularFontP, interBoldFontP ]) const { searchParams } = new URL(req.url) const pageId = searchParams.get('id') if (!pageId) { return new Response('Invalid notion page id', { status: 400 }) } const pageInfoRes = await fetch(`${apiHost}${api.getNotionPageInfo}`, { method: 'POST', body: JSON.stringify({ pageId }), headers: { 'Content-Type': 'application/json' } }) if (!pageInfoRes.ok) { return new Response(pageInfoRes.statusText, { status: pageInfoRes.status }) } const pageInfo: NotionPageInfo = await pageInfoRes.json() console.log(pageInfo) return new ImageResponse( (
{pageInfo.image && ( )}
{pageInfo.detail && (
{pageInfo.detail}
)}
{pageInfo.title}
{pageInfo.detail && (
{pageInfo.detail}
)}
{pageInfo.authorImage && (
)}
), { width: 1200, height: 600, fonts: [ { name: 'Inter', data: interRegularFont, style: 'normal', weight: 400 }, { name: 'Inter', data: interBoldFont, style: 'normal', weight: 700 } ] } ) }