From 0d70401a8f66d9bbe978999e341fa9475479d502 Mon Sep 17 00:00:00 2001 From: imnyang Date: Sun, 19 Apr 2026 20:34:27 +0900 Subject: [PATCH] feat: make tweet field optional in PostDetailResponse and refactor metadata generation functions --- apps/frontend/src/app/detail/[id]/page.tsx | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/apps/frontend/src/app/detail/[id]/page.tsx b/apps/frontend/src/app/detail/[id]/page.tsx index 547aad5..41216df 100644 --- a/apps/frontend/src/app/detail/[id]/page.tsx +++ b/apps/frontend/src/app/detail/[id]/page.tsx @@ -13,7 +13,7 @@ type PostDetailResponse = { url?: string; author?: string; tags?: string[]; - tweet: { + tweet?: { text?: string; title?: string; }; @@ -37,6 +37,22 @@ function createDetailDescription(post: PostDetailResponse) { return `${author} | ${source} post${tagText}`; } +function createDetailTitle(post: PostDetailResponse) { + const author = post.author?.trim() || "unknown"; + const source = getSourceLabel(post.type); + const rawText = post.type === "twitter" ? post.tweet?.text : post.tweet?.title; + const contentText = rawText?.trim() || post._id; + return `${author} - ${contentText} | ${source}`; +} + +function toAbsoluteUrl(value: string, baseUrl: string) { + try { + return new URL(value, baseUrl).toString(); + } catch { + return value; + } +} + async function getApiBaseUrl() { @@ -122,12 +138,13 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin const source = getSourceLabel(post.type); const author = post.author?.trim() || "unknown"; - const title = `${author} - ${post.type == "twitter" ? post.tweet.text : post.tweet.title} | ${source}`; + const title = createDetailTitle(post); const description = createDetailDescription(post); - const ogImages = post.mediaUrl + const ogImageUrl = post.mediaUrl ? toAbsoluteUrl(post.mediaUrl, apiBaseUrl) : undefined; + const ogImages = ogImageUrl ? [ { - url: post.mediaUrl, + url: ogImageUrl, alt: `${author} ${source}`, }, ] @@ -151,7 +168,7 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin card: post.mediaUrl ? "summary_large_image" : "summary", title, description, - images: post.mediaUrl ? [post.mediaUrl] : undefined, + images: ogImageUrl ? [ogImageUrl] : undefined, }, }; } catch {