Skip to content

Commit 3f58efa

Browse files
committed
feat: 동적 메타데이터 생성 기능 구현
1 parent c82a670 commit 3f58efa

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/app/post/[id]/page.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Metadata } from 'next';
12
import { notFound } from 'next/navigation';
23

34
import { getAllPostSlugs, getPost } from '@/shared/lib';
@@ -13,6 +14,57 @@ export async function generateStaticParams() {
1314
return slugs.map((slug) => ({ id: slug }));
1415
}
1516

17+
export async function generateMetadata({
18+
params,
19+
}: PostDetailPageProps): Promise<Metadata> {
20+
const { id } = await params;
21+
const post = getPost(id);
22+
23+
if (!post) {
24+
return {
25+
title: '포스트를 찾을 수 없습니다',
26+
};
27+
}
28+
29+
const baseUrl = 'https://dobbymin.github.io';
30+
const postUrl = `${baseUrl}/post/${id}`;
31+
const thumbnailUrl = post.thumbnail
32+
? `${baseUrl}${post.thumbnail}`
33+
: `${baseUrl}/logo.png`;
34+
35+
return {
36+
title: post.title,
37+
description: post.description,
38+
keywords: [post.category, post.title, '개발 블로그'],
39+
authors: [{ name: '김도비', url: 'https://github.com/Dobbymin' }],
40+
creator: '김도비',
41+
openGraph: {
42+
type: 'article',
43+
locale: 'ko_KR',
44+
url: postUrl,
45+
title: post.title,
46+
description: post.description,
47+
siteName: 'Dobby is Free',
48+
publishedTime: post.date,
49+
authors: ['김도비'],
50+
images: [
51+
{
52+
url: thumbnailUrl,
53+
width: 1200,
54+
height: 630,
55+
alt: post.title,
56+
},
57+
],
58+
},
59+
twitter: {
60+
card: 'summary_large_image',
61+
title: post.title,
62+
description: post.description,
63+
images: [thumbnailUrl],
64+
},
65+
};
66+
}
67+
1668
export default async function PostDetailPage({ params }: PostDetailPageProps) {
1769
const { id } = await params;
1870
const post = getPost(id);

0 commit comments

Comments
 (0)