Skip to content

Commit

Permalink
Merge pull request #1082 from shelton-xiaoteng-ma/add-pagination-to-tags
Browse files Browse the repository at this point in the history
feat(pagination): Implement pagination for tag pages
  • Loading branch information
timlrx authored Jan 9, 2025
2 parents f1eb945 + 088754f commit 9862697
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
10 changes: 8 additions & 2 deletions app/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import ListLayout from '@/layouts/ListLayoutWithTags'
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer'
import { allBlogs } from 'contentlayer/generated'
import { genPageMetadata } from 'app/seo'
import { notFound } from 'next/navigation'

const POSTS_PER_PAGE = 5

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

export default function BlogPage() {
export default async function BlogPage(props: { searchParams: Promise<{ page: string }> }) {
const posts = allCoreContent(sortPosts(allBlogs))
const pageNumber = 1
const searchParams = await props.searchParams
const pageNumber = parseInt(searchParams.page || '1')
const initialDisplayPosts = posts.slice(
POSTS_PER_PAGE * (pageNumber - 1),
POSTS_PER_PAGE * pageNumber
)
if (initialDisplayPosts.length === 0) {
return notFound()
}

const pagination = {
currentPage: pageNumber,
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
Expand Down
35 changes: 0 additions & 35 deletions app/blog/page/[page]/page.tsx

This file was deleted.

31 changes: 28 additions & 3 deletions app/tags/[tag]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { genPageMetadata } from 'app/seo'
import { Metadata } from 'next'
import { notFound } from 'next/navigation'

const POSTS_PER_PAGE = 5

export async function generateMetadata(props: {
params: Promise<{ tag: string }>
}): Promise<Metadata> {
Expand All @@ -34,16 +36,39 @@ export const generateStaticParams = async () => {
return paths
}

export default async function TagPage(props: { params: Promise<{ tag: string }> }) {
export default async function TagPage(props: {
params: Promise<{ tag: string }>
searchParams: Promise<{ page: string }>
}) {
const params = await props.params
const tag = decodeURI(params.tag)
const searchParams = await props.searchParams
const pageNumber = parseInt(searchParams.page || '1')
// Capitalize first letter and convert space to dash
const title = tag[0].toUpperCase() + tag.split(' ').join('-').slice(1)
const filteredPosts = allCoreContent(
sortPosts(allBlogs.filter((post) => post.tags && post.tags.map((t) => slug(t)).includes(tag)))
)
if (filteredPosts.length === 0) {
const initialDisplayPosts = filteredPosts.slice(
POSTS_PER_PAGE * (pageNumber - 1),
POSTS_PER_PAGE * pageNumber
)

if (initialDisplayPosts.length === 0) {
return notFound()
}
return <ListLayout posts={filteredPosts} title={title} />

const pagination = {
currentPage: pageNumber,
totalPages: Math.ceil(filteredPosts.length / POSTS_PER_PAGE),
}

return (
<ListLayout
posts={filteredPosts}
initialDisplayPosts={initialDisplayPosts}
pagination={pagination}
title={title}
/>
)
}
5 changes: 2 additions & 3 deletions layouts/ListLayoutWithTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface ListLayoutProps {

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

Expand All @@ -37,7 +36,7 @@ function Pagination({ totalPages, currentPage }: PaginationProps) {
)}
{prevPage && (
<Link
href={currentPage - 1 === 1 ? `/${basePath}/` : `/${basePath}/page/${currentPage - 1}`}
href={currentPage - 1 === 1 ? `${pathname}/` : `${pathname}/?page=${currentPage - 1}`}
rel="prev"
>
Previous
Expand All @@ -52,7 +51,7 @@ function Pagination({ totalPages, currentPage }: PaginationProps) {
</button>
)}
{nextPage && (
<Link href={`/${basePath}/page/${currentPage + 1}`} rel="next">
<Link href={`${pathname}/?page=${currentPage + 1}`} rel="next">
Next
</Link>
)}
Expand Down

0 comments on commit 9862697

Please sign in to comment.