Next.js: Handle 404s
13 June 2025 (Updated 13 June 2025)
Uset the notFound()
helper function within a route to display the not-found.tsx
file:
import { getPostBySlug } from '@/lib/posts'
export default async function Page({ params }: { params: { slug: string } }) {
const { slug } = await params
const post = getPostBySlug(slug)
if (!post) {
notFound()
}
return <div>{post.title}</div>
}
Tagged:
Next.js