-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1091 from timlrx/tag-paginated
static paginated tag pages
- Loading branch information
Showing
8 changed files
with
1,830 additions
and
1,672 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ListLayout from '@/layouts/ListLayoutWithTags' | ||
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer' | ||
import { allBlogs } from 'contentlayer/generated' | ||
import { notFound } from 'next/navigation' | ||
|
||
const POSTS_PER_PAGE = 5 | ||
|
||
export const generateStaticParams = async () => { | ||
const totalPages = Math.ceil(allBlogs.length / POSTS_PER_PAGE) | ||
const paths = Array.from({ length: totalPages }, (_, i) => ({ page: (i + 1).toString() })) | ||
|
||
return paths | ||
} | ||
|
||
export default async function Page(props: { params: Promise<{ page: string }> }) { | ||
const params = await props.params | ||
const posts = allCoreContent(sortPosts(allBlogs)) | ||
const pageNumber = parseInt(params.page as string) | ||
const totalPages = Math.ceil(posts.length / POSTS_PER_PAGE) | ||
|
||
// Return 404 for invalid page numbers or empty pages | ||
if (pageNumber <= 0 || pageNumber > totalPages || isNaN(pageNumber)) { | ||
return notFound() | ||
} | ||
const initialDisplayPosts = posts.slice( | ||
POSTS_PER_PAGE * (pageNumber - 1), | ||
POSTS_PER_PAGE * pageNumber | ||
) | ||
const pagination = { | ||
currentPage: pageNumber, | ||
totalPages: totalPages, | ||
} | ||
|
||
return ( | ||
<ListLayout | ||
posts={posts} | ||
initialDisplayPosts={initialDisplayPosts} | ||
pagination={pagination} | ||
title="All Posts" | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { slug } from 'github-slugger' | ||
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer' | ||
import ListLayout from '@/layouts/ListLayoutWithTags' | ||
import { allBlogs } from 'contentlayer/generated' | ||
import tagData from 'app/tag-data.json' | ||
import { notFound } from 'next/navigation' | ||
|
||
const POSTS_PER_PAGE = 5 | ||
|
||
export const generateStaticParams = async () => { | ||
const tagCounts = tagData as Record<string, number> | ||
return Object.keys(tagCounts).flatMap((tag) => { | ||
const postCount = tagCounts[tag] | ||
const totalPages = Math.ceil(postCount / POSTS_PER_PAGE) | ||
return Array.from({ length: totalPages - 1 }, (_, i) => ({ | ||
tag: encodeURI(tag), | ||
page: (i + 2).toString(), | ||
})) | ||
}) | ||
} | ||
|
||
export default async function TagPage(props: { params: Promise<{ tag: string; page: string }> }) { | ||
const params = await props.params | ||
const tag = decodeURI(params.tag) | ||
const title = tag[0].toUpperCase() + tag.split(' ').join('-').slice(1) | ||
const pageNumber = parseInt(params.page) | ||
const filteredPosts = allCoreContent( | ||
sortPosts(allBlogs.filter((post) => post.tags && post.tags.map((t) => slug(t)).includes(tag))) | ||
) | ||
const totalPages = Math.ceil(filteredPosts.length / POSTS_PER_PAGE) | ||
|
||
// Return 404 for invalid page numbers or empty pages | ||
if (pageNumber <= 0 || pageNumber > totalPages || isNaN(pageNumber)) { | ||
return notFound() | ||
} | ||
const initialDisplayPosts = filteredPosts.slice( | ||
POSTS_PER_PAGE * (pageNumber - 1), | ||
POSTS_PER_PAGE * pageNumber | ||
) | ||
const pagination = { | ||
currentPage: pageNumber, | ||
totalPages: totalPages, | ||
} | ||
|
||
return ( | ||
<ListLayout | ||
posts={filteredPosts} | ||
initialDisplayPosts={initialDisplayPosts} | ||
pagination={pagination} | ||
title={title} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.