feat: optimize taxonomy data retrieval and streamline pagination handling

This commit is contained in:
Thuan Bui
2026-03-19 17:54:59 +07:00
parent 7919848fcd
commit 54f84310ad
8 changed files with 10 additions and 79 deletions
-1
View File
@@ -35,7 +35,6 @@ export async function getStaticPaths() {
params: { rest: pageParam ? `${fullPath}/${pageParam}` : fullPath },
props: {
cat,
fullPath,
posts: page.data,
currentPage: page.currentPage,
lastPage: page.lastPage,
@@ -1,71 +0,0 @@
---
import BaseLayout from "@/layouts/BaseLayout.astro";
import { getCollection } from "astro:content";
import Headline from "@/components/ui/Headline.astro";
import PostItem from "@/components/blog/PostItem.astro";
import Pagination from "@/components/ui/Pagination.astro";
import { getTaxonomyMap } from "@/utils/taxonomy";
import { buildPaginatedPaths } from "@/utils/paginate";
export async function getStaticPaths() {
const allPosts = await getCollection("blog");
const posts = allPosts.filter((post) => post.data && post.data.pubDate);
const categories = new Set<string>();
posts.forEach((post) => {
post.data.categories?.forEach((cat) => categories.add(cat));
});
return Array.from(categories).flatMap((category) => {
const categoryPosts = posts
.filter((post) => post.data.categories?.includes(category))
.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf());
return buildPaginatedPaths(categoryPosts, `/category/${category}`).map(
({ pageParam, page }) => ({
params: { category, page: pageParam },
props: { page },
}),
);
});
}
const { page } = Astro.props;
const { category } = Astro.params;
const categoryInfo = getTaxonomyMap("category").get(category!);
const displayName = categoryInfo?.name ?? category!;
const description = categoryInfo?.description;
const metadata = {
title: displayName,
description: description ?? `Các bài viết trong danh mục ${displayName}.`,
};
---
<BaseLayout metadata={metadata}>
<section class="relative px-4 py-8 sm:px-6 lg:px-8 md:py-12">
<div class="relative mx-auto max-w-5xl">
<Headline
tagline="Category"
title={displayName}
subtitle={description ?? `Hiển thị ${page.total} bài viết.`}
classes={{
container: "mb-12 text-center",
title:
"font-heading mb-4 text-4xl font-bold tracking-tight text-foreground md:text-6xl capitalized",
subtitle: "mx-auto max-w-3xl text-xl text-muted-foreground",
}}
/>
<div class="mb-8 text-center">
<a href="/blog" class="text-primary hover:underline">← Quay lại blog</a>
</div>
<div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
{page.data.map((post) => <PostItem post={post} />)}
</div>
<Pagination page={page} />
</div>
</section>
</BaseLayout>
-1
View File
@@ -35,7 +35,6 @@ export async function getStaticPaths() {
params: { rest: pageParam ? `${fullPath}/${pageParam}` : fullPath },
props: {
dest,
fullPath,
posts: page.data,
currentPage: page.currentPage,
lastPage: page.lastPage,