Skip to content

Commit 9862697

Browse files
authored
Merge pull request #1082 from shelton-xiaoteng-ma/add-pagination-to-tags
feat(pagination): Implement pagination for tag pages
2 parents f1eb945 + 088754f commit 9862697

File tree

4 files changed

+38
-43
lines changed

4 files changed

+38
-43
lines changed

app/blog/page.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@ import ListLayout from '@/layouts/ListLayoutWithTags'
22
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer'
33
import { allBlogs } from 'contentlayer/generated'
44
import { genPageMetadata } from 'app/seo'
5+
import { notFound } from 'next/navigation'
56

67
const POSTS_PER_PAGE = 5
78

89
export const metadata = genPageMetadata({ title: 'Blog' })
910

10-
export default function BlogPage() {
11+
export default async function BlogPage(props: { searchParams: Promise<{ page: string }> }) {
1112
const posts = allCoreContent(sortPosts(allBlogs))
12-
const pageNumber = 1
13+
const searchParams = await props.searchParams
14+
const pageNumber = parseInt(searchParams.page || '1')
1315
const initialDisplayPosts = posts.slice(
1416
POSTS_PER_PAGE * (pageNumber - 1),
1517
POSTS_PER_PAGE * pageNumber
1618
)
19+
if (initialDisplayPosts.length === 0) {
20+
return notFound()
21+
}
22+
1723
const pagination = {
1824
currentPage: pageNumber,
1925
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),

app/blog/page/[page]/page.tsx

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/tags/[tag]/page.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { genPageMetadata } from 'app/seo'
88
import { Metadata } from 'next'
99
import { notFound } from 'next/navigation'
1010

11+
const POSTS_PER_PAGE = 5
12+
1113
export async function generateMetadata(props: {
1214
params: Promise<{ tag: string }>
1315
}): Promise<Metadata> {
@@ -34,16 +36,39 @@ export const generateStaticParams = async () => {
3436
return paths
3537
}
3638

37-
export default async function TagPage(props: { params: Promise<{ tag: string }> }) {
39+
export default async function TagPage(props: {
40+
params: Promise<{ tag: string }>
41+
searchParams: Promise<{ page: string }>
42+
}) {
3843
const params = await props.params
3944
const tag = decodeURI(params.tag)
45+
const searchParams = await props.searchParams
46+
const pageNumber = parseInt(searchParams.page || '1')
4047
// Capitalize first letter and convert space to dash
4148
const title = tag[0].toUpperCase() + tag.split(' ').join('-').slice(1)
4249
const filteredPosts = allCoreContent(
4350
sortPosts(allBlogs.filter((post) => post.tags && post.tags.map((t) => slug(t)).includes(tag)))
4451
)
45-
if (filteredPosts.length === 0) {
52+
const initialDisplayPosts = filteredPosts.slice(
53+
POSTS_PER_PAGE * (pageNumber - 1),
54+
POSTS_PER_PAGE * pageNumber
55+
)
56+
57+
if (initialDisplayPosts.length === 0) {
4658
return notFound()
4759
}
48-
return <ListLayout posts={filteredPosts} title={title} />
60+
61+
const pagination = {
62+
currentPage: pageNumber,
63+
totalPages: Math.ceil(filteredPosts.length / POSTS_PER_PAGE),
64+
}
65+
66+
return (
67+
<ListLayout
68+
posts={filteredPosts}
69+
initialDisplayPosts={initialDisplayPosts}
70+
pagination={pagination}
71+
title={title}
72+
/>
73+
)
4974
}

layouts/ListLayoutWithTags.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ interface ListLayoutProps {
2323

2424
function Pagination({ totalPages, currentPage }: PaginationProps) {
2525
const pathname = usePathname()
26-
const basePath = pathname.split('/')[1]
2726
const prevPage = currentPage - 1 > 0
2827
const nextPage = currentPage + 1 <= totalPages
2928

@@ -37,7 +36,7 @@ function Pagination({ totalPages, currentPage }: PaginationProps) {
3736
)}
3837
{prevPage && (
3938
<Link
40-
href={currentPage - 1 === 1 ? `/${basePath}/` : `/${basePath}/page/${currentPage - 1}`}
39+
href={currentPage - 1 === 1 ? `${pathname}/` : `${pathname}/?page=${currentPage - 1}`}
4140
rel="prev"
4241
>
4342
Previous
@@ -52,7 +51,7 @@ function Pagination({ totalPages, currentPage }: PaginationProps) {
5251
</button>
5352
)}
5453
{nextPage && (
55-
<Link href={`/${basePath}/page/${currentPage + 1}`} rel="next">
54+
<Link href={`${pathname}/?page=${currentPage + 1}`} rel="next">
5655
Next
5756
</Link>
5857
)}

0 commit comments

Comments
 (0)