diff --git a/src/app/article/[slug]/page.tsx b/src/app/article/[slug]/page.tsx index 74e5733..bf8b19b 100644 --- a/src/app/article/[slug]/page.tsx +++ b/src/app/article/[slug]/page.tsx @@ -2,11 +2,12 @@ import Header from '@/components/Header'; import styles from './article.module.css'; import Tag from '@/components/Tag'; import Image from "next/image"; -import { articleTypes, getArticle } from '@/utils/news'; +import { articleTypes, generateBannerURL, getArticle } from '@/utils/news'; import { NEWS_DEFAULT_BANNER, NEWS_IMAGES_URL } from '@/utils/constants'; import Footer from '@/components/Footer'; import Markdown from 'react-markdown'; import Author from '@/components/News/Author'; +import { Metadata, ResolvingMetadata } from 'next'; type Props = { params: { @@ -14,6 +15,18 @@ type Props = { } }; +export async function generateMetadata({params}: Props, parent: ResolvingMetadata): Promise { + const article = await getArticle(params.slug); + const previousImages = (await parent).openGraph?.images || []; + + return { + title: article.title, + openGraph: { + images: article.banner ? generateBannerURL(article.banner) : previousImages + } + } +} + const ArticlePage: React.FC = async ({ params }) => { const article = await getArticle(params.slug); const type = articleTypes[article.type]; @@ -26,7 +39,7 @@ const ArticlePage: React.FC = async ({ params }) => {
- Article Banner + Article Banner
diff --git a/src/utils/news.ts b/src/utils/news.ts index 6384e90..6d4f836 100644 --- a/src/utils/news.ts +++ b/src/utils/news.ts @@ -1,5 +1,5 @@ import { TagColor } from "@/components/Tag" -import { NEWS_SOURCE } from "./constants" +import { NEWS_IMAGES_URL, NEWS_SOURCE } from "./constants" import { z } from "zod"; import matter from "gray-matter"; @@ -101,4 +101,8 @@ export const getArticle = async (slug: string): Promise
=> { export const getAuthorData = async (id: string) => { const raw: ArticleAuthor = await fetch(`${NEWS_SOURCE}/authors/${id}.json`, { next: { tags: ["articles", `article-author-${id}`] } }).then(res => res.json()); return ArticleAuthor.parse(raw); +} + +export const generateBannerURL = (url: string) => { + return `${NEWS_IMAGES_URL}/banners/${url}`; } \ No newline at end of file