Skip to content

Commit

Permalink
feat: use notFound()
Browse files Browse the repository at this point in the history
  • Loading branch information
junkisai committed Oct 7, 2024
1 parent bc13031 commit b18a23f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { langSchema, langs } from '@/i18n'
import { findPostByLangAndSlug } from '@/utils/posts'
import { allPosts } from 'contentlayer/generated'
import { format, parseISO } from 'date-fns'
import { notFound } from 'next/navigation'
import { object, parse, string } from 'valibot'

export const generateStaticParams = async () => {
Expand All @@ -15,7 +16,7 @@ export const generateMetadata = ({ params }: PageProps) => {
const { lang, slug } = parse(paramsSchema, params)
const post = findPostByLangAndSlug({ lang, slug })

if (!post) throw new Error(`Post not found for slug: ${slug}`)
if (!post) notFound()

return { title: post.title }
}
Expand All @@ -29,7 +30,7 @@ export default function Page({ params }: PageProps) {
const { lang, slug } = parse(paramsSchema, params)

const post = findPostByLangAndSlug({ lang, slug })
if (!post) throw new Error(`Post not found for slug: ${slug}`)
if (!post) notFound()

return (
<article>
Expand Down
5 changes: 3 additions & 2 deletions frontend/apps/service-site/src/app/posts/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fallbackLang } from '@/i18n'
import { findPostByLangAndSlug } from '@/utils/posts'
import { allPosts } from 'contentlayer/generated'
import { format, parseISO } from 'date-fns'
import { notFound } from 'next/navigation'
import { object, parse, string } from 'valibot'

export const generateStaticParams = async () =>
Expand All @@ -12,7 +13,7 @@ export const generateMetadata = ({ params }: PageProps) => {
const { slug } = parse(paramsSchema, params)
const post = findPostByLangAndSlug({ lang: fallbackLang, slug })

if (!post) throw new Error(`Post not found for slug: ${slug}`)
if (!post) notFound()

return { title: post.title }
}
Expand All @@ -25,7 +26,7 @@ export default function Page({ params }: PageProps) {
const { slug } = parse(paramsSchema, params)

const post = findPostByLangAndSlug({ lang: fallbackLang, slug })
if (!post) throw new Error(`Post not found for slug: ${slug}`)
if (!post) notFound()

return (
<article>
Expand Down

0 comments on commit b18a23f

Please sign in to comment.