Skip to content

Commit e1fd81e

Browse files
fix the author and adding notFound error
1 parent 1d10e24 commit e1fd81e

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

app/author/[slug]/page.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import React from 'react';
22
import { Card } from '@/components/Card/Card';
33
import _ from 'lodash';
4-
import { allPosts, Post } from 'contentlayer/generated';
4+
import { allPosts } from 'contentlayer/generated';
55
import type { Metadata } from 'next'
6+
import type { Post } from 'contentlayer/generated';
7+
import { notFound } from 'next/navigation';
8+
69

710
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
811
const { slug } = await params
@@ -17,7 +20,7 @@ export async function generateStaticParams() {
1720
allPosts.map(
1821
item => {
1922
if (item.author !== undefined) {
20-
paths.push({ slug: item.author?.name.trim().toLowerCase().replaceAll(" ", "-") })
23+
paths.push({ slug: item.author?.trim().toLowerCase().replaceAll(" ", "-") })
2124
}
2225
}
2326
)
@@ -29,18 +32,19 @@ export async function generateStaticParams() {
2932
)
3033
return RemoveDuplicateAuthor
3134
}
32-
export default async function Page({ params }: { params: Promise<{ slug: string }> }){
35+
export default async function Page({ params }: { params: Promise<{ slug: string }> }) {
3336

3437
const slug = (await params).slug
3538

3639
const AuthorPosts: Post[] = allPosts.filter((post) => {
3740
if (post.author !== undefined) {
38-
if (post.author?.name.trim().toLowerCase().replaceAll(" ", "-") === slug) return post
41+
if (post.author?.trim().toLowerCase().replaceAll(" ", "-") === slug) return post
3942
}
4043
})
4144

42-
if (AuthorPosts.length === 0) throw new Error(`Author not found for slug: ${slug}`)
43-
45+
if (AuthorPosts.length === 0) {
46+
notFound()
47+
}
4448

4549
return (
4650
<section className="py-32">

app/read/[slug]/page.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { allPosts } from 'contentlayer/generated';
44
import _ from 'lodash';
55
import dayjs from "dayjs";
66
import Image from 'next/image';
7+
import { notFound } from 'next/navigation';
78

89
export const generateStaticParams = async () => allPosts.map((post) => ({ slug: post._raw.flattenedPath }))
910

@@ -14,7 +15,9 @@ export async function generateMetadata( { params }: { params: Promise<{ slug: st
1415

1516
const post = allPosts.find((post) => post._raw.flattenedPath === slug)
1617

17-
if (!post) throw new Error(`Post not found for slug: ${slug}`)
18+
if (!post) {
19+
notFound()
20+
}
1821

1922
return { title: post.title }
2023

@@ -24,7 +27,9 @@ export default async function Page({ params }: { params: Promise<{ slug: string
2427
const slug = (await params).slug
2528
const post = allPosts.find((post) => post._raw.flattenedPath === slug)
2629

27-
if (!post) throw new Error(`Post not found for slug: ${slug}`)
30+
if (!post) {
31+
notFound()
32+
}
2833

2934
return (
3035
<main className="my-16 pt-8 pb-16 lg:pt-16 lg:pb-24">
@@ -39,8 +44,8 @@ export default async function Page({ params }: { params: Promise<{ slug: string
3944

4045
<div className="flex items-center justify-between mt-4 mb-6 not-italic">
4146

42-
<Link href={`/author/${post.author?.name.trim().toLowerCase().replaceAll(" ", "-")}`} rel="author" className="no-underline">
43-
Published By {post.author?.name}
47+
<Link href={`/author/${post.author?.trim().toLowerCase().replaceAll(" ", "-")}`} rel="author" className="no-underline">
48+
Published By {post?.author}
4449
</Link>
4550

4651
<time dateTime={post.date} title={dayjs(post.date).format("MMM DD, YYYY")} className="text-base text-primary">

0 commit comments

Comments
 (0)