Load a published article by slug, render its Markdown, and handle missing content and API errors.
Use the server-only API helper and styles from setup. Create this file at app/blog/[slug]/page.tsx. It loads the published article for the slug, renders its content, and generates metadata from the article record. The async params match the current App Router interface.
import type { Metadata } from "next";
import Link from "next/link";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { getArticle, websiteUrl } from "../../../lib/content";
import styles from "../../blog.module.css";
export async function generateMetadata({
params,
}: {
params: Promise<{ slug: string }>;
}): Promise<Metadata> {
const a = await getArticle((await params).slug);
const title = a.seoTitle || a.title;
const description = a.seoDescription || a.excerpt;
const url = `${websiteUrl}/blog/${a.slug}`;
const image = a.socialImageUrl || a.coverUrl;
return {
title,
description,
alternates: { canonical: url },
openGraph: {
type: "article",
title,
description,
url,
publishedTime: a.publishedAt,
modifiedTime: a.updatedAt,
authors: [a.author],
tags: a.tags,
images: image ? [image] : [],
},
twitter: {
card: "summary_large_image",
title,
description,
images: image ? [image] : [],
},
};
}
export default async function ArticlePage({
params,
}: {
params: Promise<{ slug: string }>;
}) {
const a = await getArticle((await params).slug);
const jsonLd = {
"@context": "https://schema.org",
"@type": "Article",
headline: a.title,
description: a.seoDescription || a.excerpt,
datePublished: a.publishedAt,
dateModified: a.updatedAt,
author: { "@type": "Person", name: a.author },
image: a.socialImageUrl || a.coverUrl || undefined,
mainEntityOfPage: `${websiteUrl}/blog/${a.slug}`,
};
return (
<article className={styles.article}>
<Link href="/blog">← All articles</Link>
<h1>{a.title}</h1>
<div className={styles.meta}>
<time dateTime={a.articleDate}>{a.articleDate}</time> · {a.author}
</div>
{a.coverUrl && <img src={a.coverUrl} alt={a.title} />}
<ReactMarkdown remarkPlugins={[remarkGfm]} skipHtml>
{a.body}
</ReactMarkdown>
<div>
{a.tags.map((t) => (
<Link
className={styles.tag}
key={t}
href={`/blog?tag=${encodeURIComponent(t)}`}
>
{t}
</Link>
))}
</div>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify(jsonLd).replace(/</g, "\\u003c"),
}}
/>
</article>
);
}
react-markdown renders the body with raw HTML disabled by skipHtml. Its default URL handling rejects unsafe schemes. remark-gfm adds tables, task lists, and other GitHub-flavored Markdown features. Keep raw HTML disabled and retain safe URL handling.
When an article is published, Postparticle resolves valid media references to public delivery URLs. The API supplies coverUrl and socialImageUrl; it does not return private object keys. Add alt text to Markdown images. The example uses the article title as cover alt text. Video references render as links; add a player if your site needs one.
The helper calls Next.js notFound() when an article is missing or unpublished. HTTP 400 and 503 responses and network failures propagate to the error boundary. Add app/error.tsx to show a recovery message.
Saving a draft does not change the published article. The website receives edits only after an explicit publish, subject to its cache settings.