From 7faaac2081427f1f8242d3c45bfd587f551ac7ad Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Tue, 31 Mar 2026 02:39:33 +0200 Subject: [PATCH] fix: notFound() on missing post, await params for Next.js 15 --- app/posts/[slug]/page.tsx | 10 +++++----- lib/posts.ts | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/posts/[slug]/page.tsx b/app/posts/[slug]/page.tsx index bc9e184..0e6e55a 100644 --- a/app/posts/[slug]/page.tsx +++ b/app/posts/[slug]/page.tsx @@ -8,9 +8,7 @@ import rehypePrettyCode from "rehype-pretty-code"; import rehypeSlug from "rehype-slug"; interface PageProps { - params: { - slug: string; - }; + params: Promise<{ slug: string }>; } export async function generateStaticParams() { @@ -19,14 +17,16 @@ export async function generateStaticParams() { } export async function generateMetadata({ params }: PageProps) { - const postData = await getPostData(params.slug); + const { slug } = await params; + const postData = await getPostData(slug); return { title: `${postData.title} | Gabriel's Kaszewski Blog`, }; } export default async function Post({ params }: PageProps) { - const postData: PostData = await getPostData(params.slug); + const { slug } = await params; + const postData: PostData = await getPostData(slug); return (
diff --git a/lib/posts.ts b/lib/posts.ts index aec81b4..4877cfb 100644 --- a/lib/posts.ts +++ b/lib/posts.ts @@ -2,6 +2,7 @@ import fs from 'fs'; import path from 'path'; import matter from 'gray-matter'; import readingTime from 'reading-time'; +import { notFound } from 'next/navigation'; const postsDirectory = path.join(process.cwd(), 'posts'); @@ -90,6 +91,7 @@ export function getAllPostIds() { export async function getPostData(id: string): Promise { const fullPath = path.join(postsDirectory, `${id}.mdx`); + if (!fs.existsSync(fullPath)) notFound(); const fileContents = fs.readFileSync(fullPath, 'utf8'); const matterResult = matter(fileContents); const stats = readingTime(matterResult.content);