From f85ffe4ea6ecbe7b04e5befff39a28b0952e7ec6 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 19 Jul 2023 00:48:12 +0800 Subject: [PATCH] feat: add og (#792) --- package.json | 2 +- src/app/api/og/route.tsx | 182 ++++++++++++++++++++++++++++ src/app/site/[site]/[slug]/page.tsx | 5 +- src/components/home/PostCover.tsx | 20 ++- 4 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 src/app/api/og/route.tsx diff --git a/package.json b/package.json index 2e9aad8a..8de55a1f 100644 --- a/package.json +++ b/package.json @@ -211,4 +211,4 @@ "minimumChangeThreshold": 0, "showDetails": true } -} +} \ No newline at end of file diff --git a/src/app/api/og/route.tsx b/src/app/api/og/route.tsx new file mode 100644 index 00000000..c1c54a9a --- /dev/null +++ b/src/app/api/og/route.tsx @@ -0,0 +1,182 @@ +import { ImageResponse, type NextRequest } from "next/server" +import uniqolor from "uniqolor" + +import { gql } from "@urql/core" + +import { toGateway } from "~/lib/ipfs-parser" +import { client } from "~/queries/graphql" + +const fontNormal = fetch( + "https://github.com/lxgw/LxgwWenKai/releases/download/v1.300/LXGWWenKai-Regular.ttf", +).then((res) => res.arrayBuffer()) + +export const runtime = "edge" + +export const revalidate = 60 * 60 * 24 // 24 hours +export const GET = async (req: NextRequest) => { + try { + const fontData = await fontNormal + + // api/og?characterId=52055¬eId=286&site=innei-4525 + const noteId = req.nextUrl.searchParams.get("noteId") + const characterId = req.nextUrl.searchParams.get("characterId") + + if (!noteId || !characterId) + return new Response(`Missing noteId or characterId`, { status: 400 }) + + const result = await client + .query( + gql` + query getNote($characterId: Int!, $noteId: Int!) { + note( + where: { + note_characterId_noteId_unique: { + characterId: $characterId + noteId: $noteId + } + } + ) { + metadata { + content + } + character { + handle + metadata { + content + } + } + } + } + `, + { + characterId: parseInt(characterId), + noteId: parseInt(noteId), + }, + ) + .toPromise() + const title = result.data.note.metadata?.content?.title + const subtitle = result.data.note.metadata?.content?.summary?.slice(0, 15) + const avatar = toGateway( + result.data.note.character.metadata?.content?.avatars?.[0], + ) + + const siteName = + result.data.note.character.metadata?.content?.site_name || + result.data.note.character.metadata?.content?.name + + const bgAccent = uniqolor(title, { + saturation: [30, 35], + lightness: [60, 70], + }).color + + const bgAccentLight = uniqolor(title, { + saturation: [30, 35], + lightness: [80, 90], + }).color + + const bgAccentUltraLight = uniqolor(title, { + saturation: [30, 35], + lightness: [95, 96], + }).color + + return new ImageResponse( + ( +
+
+ {avatar && ( + + )} + + +

{siteName}

+
+
+
+

+ {title?.slice(0, 20)} +

+

+ {subtitle} +

+
+
+ ), + { + width: 1200, + height: 600, + fonts: [ + { + name: "LXGW WenKai Screen R", + data: fontData, + weight: 400, + style: "normal", + }, + ], + }, + ) + } catch (e: any) { + return new Response(`Failed to generate the OG image. Error ${e.message}`, { + status: 500, + }) + } +} diff --git a/src/app/site/[site]/[slug]/page.tsx b/src/app/site/[site]/[slug]/page.tsx index 2e4698a3..d8e54a23 100644 --- a/src/app/site/[site]/[slug]/page.tsx +++ b/src/app/site/[site]/[slug]/page.tsx @@ -42,9 +42,10 @@ export async function generateMetadata({ const title = `${page?.metadata?.content?.title} - ${ site?.metadata?.content?.name || site?.handle }` + const description = page?.metadata?.content?.summary - const siteImages = - site?.metadata?.content?.avatars?.[0] || `${SITE_URL}/assets/logo.svg` + const siteImages = `${SITE_URL}/api/og?noteId=${page?.noteId}&characterId=${site?.characterId}` + const images = toGateway(page?.metadata?.content?.cover || siteImages) const useLargeOGImage = !!page?.metadata?.content?.cover const twitterCreator = site?.metadata?.content?.connected_accounts diff --git a/src/components/home/PostCover.tsx b/src/components/home/PostCover.tsx index f2e01d89..515f07f2 100644 --- a/src/components/home/PostCover.tsx +++ b/src/components/home/PostCover.tsx @@ -13,15 +13,27 @@ export default function PostCover({ }) { if (!cover) { if (title) { + const bgAccent = uniqolor(title, { + saturation: [30, 35], + lightness: [60, 70], + }).color + + const bgAccentLight = uniqolor(title, { + saturation: [30, 35], + lightness: [80, 90], + }).color + + const bgAccentUltraLight = uniqolor(title, { + saturation: [30, 35], + lightness: [95, 96], + }).color + return ( <>
{title}