diff --git a/package.json b/package.json index 4ce1c9a..e471acd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,8 @@ "@plaiceholder/next": "^2.2.0", "@vercel/analytics": "^1.3.1", "animejs": "^3.2.1", + "@resvg/resvg-js": "^2.6.0", + "satori": "^0.10.0", "classnames": "^2.3.1", "date-fns": "^2.13.0", "eslint-config-next": "^14.2.15", diff --git a/pages/api/og.ts b/pages/api/og.ts new file mode 100644 index 0000000..1672148 --- /dev/null +++ b/pages/api/og.ts @@ -0,0 +1,38 @@ +import { NextApiRequest, NextApiResponse } from 'next' +import { Resvg } from '@resvg/resvg-js' +import satori from 'satori' + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const { title, description } = req.query as { title: string; description: string } + + const svg = await satori( +
+

{title}

+

{description}

+
, + { + width: 1200, + height: 630, + fonts: [], + } + ) + + const resvg = new Resvg(svg) + const pngData = resvg.render() + const pngBuffer = pngData.asPng() + + res.setHeader('Content-Type', 'image/png') + res.status(200).send(pngBuffer) +} diff --git a/pages/words/[slug].tsx b/pages/words/[slug].tsx index 5187db9..ec3081f 100644 --- a/pages/words/[slug].tsx +++ b/pages/words/[slug].tsx @@ -24,6 +24,14 @@ function WordPage({ openGraph={{ title, description, + images: [ + { + url: `/api/og?title=${encodeURIComponent(title)}&description=${encodeURIComponent(description)}`, + width: 1200, + height: 630, + alt: title, + }, + ], }} />