--- import { getTerm, getEmDashCollection, getTermsForEntries, decodeSlug, } from "emdash"; import Base from "../../layouts/Base.astro"; import PostCard from "../../components/PostCard.astro"; import { getReadingTime } from "../../utils/reading-time"; const slug = decodeSlug(Astro.params.slug); const term = slug ? await getTerm("category", slug) : null; if (!term) { return Astro.redirect("/404"); } const { entries: posts, cacheHint } = await getEmDashCollection("posts", { where: { category: term.slug }, orderBy: { published_at: "desc" }, }); Astro.cache.set(cacheHint); // Single batched query for tags on every post in this category, rather // than calling getEntryTerms() per post (which would be one round-trip // per post). const tagsByEntry = await getTermsForEntries( "posts", posts.map((p) => p.data.id), "tag", ); const filteredPosts = posts.map((post) => ({ post, tags: tagsByEntry.get(post.data.id) ?? [], })); --- Category {term.label} {filteredPosts.length} {filteredPosts.length === 1 ? "post" : "posts"} { filteredPosts.length === 0 ? ( No posts in this category yet. ) : ( {filteredPosts.map(({ post, tags }) => ( ({ slug: t.slug, label: t.label }))} /> ))} ) }
{filteredPosts.length} {filteredPosts.length === 1 ? "post" : "posts"}
No posts in this category yet.