Skip to content

Commit

Permalink
feat: og image 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Temple2001 committed Aug 20, 2024
1 parent 478f9dc commit 561453c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Container } from "@/components/layout/container";
import ScrollEvent from "@/components/ScrollEvent";
import HeaderWords from "@/components/layout/header-words";
import { blogdata } from "@/lib/constants";
import { Metadata } from "next";

export default async function ({ params }: { params: { slug: string[] } }) {
const { slug } = params;
Expand Down Expand Up @@ -73,7 +74,11 @@ export async function generateStaticParams() {
});
}

export function generateMetadata({ params }: { params: { slug: string[] } }) {
export function generateMetadata({
params,
}: {
params: { slug: string[] };
}): Metadata {
const { slug } = params;
const target = allDocuments.find((docs) => {
return slug.join("/") === docs._raw.sourceFileDir;
Expand All @@ -85,8 +90,11 @@ export function generateMetadata({ params }: { params: { slug: string[] } }) {
};
} else {
return {
title: target.title + " - " + blogdata.blogName,
title: target.title + " | " + blogdata.blogName,
description: target.description,
openGraph: {
images: [`/opengraph/${slug.join("/")}`],
},
};
}
}
90 changes: 90 additions & 0 deletions src/app/opengraph/[...slug]/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { allDocuments } from "@/contentlayer/generated";
import { ImageResponse } from "next/og";

export const size = {
width: 1200,
height: 630,
};

export async function GET(
request: Request,
{ params }: { params: { slug: string[] } }
) {
const { slug } = params;
const target = allDocuments.find((docs) => {
return slug.join("/") === docs._raw.sourceFileDir;
});

const font = fetch(
new URL(
"https://fastly.jsdelivr.net/gh/Project-Noonnu/[email protected]/Pretendard-Bold.woff"
)
).then((res) => res.arrayBuffer());

return new ImageResponse(
(
<>
{
// ImageResponse JSX element
}
<div
style={{
height: "640px",
width: "1200px",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#fff",
fontSize: 32,
}}
>
<svg
width="75"
viewBox="0 0 75 65"
fill="#000"
style={{ margin: "0 75px" }}
>
<path d="M37.59.25l36.95 64H.64l36.95-64z"></path>
</svg>
<div
style={{
fontSize: 48,
marginTop: 40,
}}
>
{target?.title}
</div>
<div
style={{
marginTop: 40,
}}
>
{target?.description}
</div>
</div>
</>
),
{
...size,
fonts: [
{
name: "Pretendard",
data: await font,
style: "normal",
},
],
}
);
}

export async function generateStaticParams() {
const docs = allDocuments;

return docs.map((doc) => {
const path = doc._raw.sourceFileDir;
return {
slug: path.split("/"),
};
});
}

0 comments on commit 561453c

Please sign in to comment.