fix: notFound() on missing post, await params for Next.js 15
This commit is contained in:
@@ -8,9 +8,7 @@ import rehypePrettyCode from "rehype-pretty-code";
|
|||||||
import rehypeSlug from "rehype-slug";
|
import rehypeSlug from "rehype-slug";
|
||||||
|
|
||||||
interface PageProps {
|
interface PageProps {
|
||||||
params: {
|
params: Promise<{ slug: string }>;
|
||||||
slug: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateStaticParams() {
|
export async function generateStaticParams() {
|
||||||
@@ -19,14 +17,16 @@ export async function generateStaticParams() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function generateMetadata({ params }: PageProps) {
|
export async function generateMetadata({ params }: PageProps) {
|
||||||
const postData = await getPostData(params.slug);
|
const { slug } = await params;
|
||||||
|
const postData = await getPostData(slug);
|
||||||
return {
|
return {
|
||||||
title: `${postData.title} | Gabriel's Kaszewski Blog`,
|
title: `${postData.title} | Gabriel's Kaszewski Blog`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function Post({ params }: PageProps) {
|
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 (
|
return (
|
||||||
<div className="mx-auto max-w-5xl">
|
<div className="mx-auto max-w-5xl">
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import matter from 'gray-matter';
|
import matter from 'gray-matter';
|
||||||
import readingTime from 'reading-time';
|
import readingTime from 'reading-time';
|
||||||
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
const postsDirectory = path.join(process.cwd(), 'posts');
|
const postsDirectory = path.join(process.cwd(), 'posts');
|
||||||
|
|
||||||
@@ -90,6 +91,7 @@ export function getAllPostIds() {
|
|||||||
|
|
||||||
export async function getPostData(id: string): Promise<PostData> {
|
export async function getPostData(id: string): Promise<PostData> {
|
||||||
const fullPath = path.join(postsDirectory, `${id}.mdx`);
|
const fullPath = path.join(postsDirectory, `${id}.mdx`);
|
||||||
|
if (!fs.existsSync(fullPath)) notFound();
|
||||||
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
||||||
const matterResult = matter(fileContents);
|
const matterResult = matter(fileContents);
|
||||||
const stats = readingTime(matterResult.content);
|
const stats = readingTime(matterResult.content);
|
||||||
|
|||||||
Reference in New Issue
Block a user