From a68a45497bf2b933735635ea29593da2f22c2f8c Mon Sep 17 00:00:00 2001 From: saisab29 Date: Tue, 10 Dec 2024 11:33:42 +0545 Subject: [PATCH] Add metadata and opengraph integration for individual project page --- src/app/api/og/route.tsx | 25 +++++++++++++++++------ src/app/projects/[id]/page.tsx | 37 ++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/app/api/og/route.tsx b/src/app/api/og/route.tsx index e8e82c9..1c53378 100644 --- a/src/app/api/og/route.tsx +++ b/src/app/api/og/route.tsx @@ -1,7 +1,8 @@ +/* eslint-disable @next/next/no-img-element */ /* eslint-disable jsx-a11y/alt-text */ /* eslint-enable jsx-a11y/alt-text */ -import Image from "next/image"; + import { ImageResponse } from "next/og"; export const runtime = "edge"; @@ -12,6 +13,11 @@ export async function GET(request: Request) { const hasTitle = searchParams.has("title"); const title = hasTitle ? searchParams.get("title") : "Cardano API"; + const hasDescirption = searchParams.has("description"); + const description = hasDescirption + ? searchParams.get("description") + : "A list of Cardano API project"; + // Fetch the Roboto font const fontData = await fetch( "https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Me5Q.ttf" @@ -39,7 +45,7 @@ export async function GET(request: Request) { backgroundColor: "#bcd7e5", }} > - Cardano API -
+ /> +

{title}

- Explore a list of Cardano API Projects + {description}

diff --git a/src/app/projects/[id]/page.tsx b/src/app/projects/[id]/page.tsx index 02c3511..889d635 100644 --- a/src/app/projects/[id]/page.tsx +++ b/src/app/projects/[id]/page.tsx @@ -1,5 +1,3 @@ -// app/projects/[id]/page.tsx - import Image from "next/image"; import data from "../../../data.json"; import SimilarProjects from "@/app/Component/SimilarProjects"; @@ -10,6 +8,41 @@ type Props = { params: Promise<{ id: string }>; }; +export async function generateMetadata({ params }: Props) { + const { id } = await params; + if (!id) { + return {}; + } + + const project = data.find((project) => project.id === id); + + if (!project) { + return {}; + } + + const projectName = project.projectName || "Cardano API"; + const projectDescription = + project.description || "A List of Cardano API Projects"; + + return { + title: projectName, + description: projectDescription, + openGraph: { + title: projectName, + description: projectDescription, + images: [ + { + url: `/api/og?title=${encodeURIComponent( + projectName + )}&description=${encodeURIComponent(projectDescription)}`, + width: 1200, + height: 630, + }, + ], + }, + }; +} + const ProjectPage = async ({ params }: Props) => { // Ensure `params` has an id