feat: make tweet field optional in PostDetailResponse and refactor metadata generation functions

This commit is contained in:
암냥 2026-04-19 20:34:27 +09:00
commit 0d70401a8f
No known key found for this signature in database

View file

@ -13,7 +13,7 @@ type PostDetailResponse = {
url?: string; url?: string;
author?: string; author?: string;
tags?: string[]; tags?: string[];
tweet: { tweet?: {
text?: string; text?: string;
title?: string; title?: string;
}; };
@ -37,6 +37,22 @@ function createDetailDescription(post: PostDetailResponse) {
return `${author} | ${source} post${tagText}`; 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() { async function getApiBaseUrl() {
@ -122,12 +138,13 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin
const source = getSourceLabel(post.type); const source = getSourceLabel(post.type);
const author = post.author?.trim() || "unknown"; 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 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}`, alt: `${author} ${source}`,
}, },
] ]
@ -151,7 +168,7 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin
card: post.mediaUrl ? "summary_large_image" : "summary", card: post.mediaUrl ? "summary_large_image" : "summary",
title, title,
description, description,
images: post.mediaUrl ? [post.mediaUrl] : undefined, images: ogImageUrl ? [ogImageUrl] : undefined,
}, },
}; };
} catch { } catch {