If you have too many articles inside one tag, it makes sense to create paginated tag pages. That’s a mouthful, but it simply looks like this if the tag is astro.
- Page 1:
/tags/astro/ - Page 2:
/tags/astro/2/ - Page 3:
/tags/astro/3/ - And so on…
This is actually somewhat complex, so let’s break it down step by step…
But if you don’t need to know the step-by-step, skip to the end and just use the code!
The URL Structure
We need both [tag] and [...page] in the URL structure. It looks like this:
src/pages/tags/└── [tag]/ └── [...page].astro # Creates pages, like /tags/astro/, /tags/astro/2/, etc.Creating pages
We need two things to create the right pages:
- The
[tag]value - The
[...page]number
The second one is created by Astro’s paginate function automatically. So we just need to pass the tag value into the params. If we’re passing an astro tag, it looks like this:
---export async function getStaticPaths({ paginate }) { return paginate(files, { params: { tag: 'astro' }, })}---The hard part here is to gather the tags so the paginate function can access each tag. There are several steps involved here.
Step 1: Gathering unique tags
The first thing we have to do is gather all of the tags that are used in the collection. This can be done by accessing the frontmatter with flatMap.
const tags = files.flatMap(post => post.data.tags || [])Then we can get unique tags with Set.
const uniqueTags = [...new Set(tags)]Now we can map over these unique tags and return paginate so the tags are no longer hard coded.
return uniqueTags.flatMap(tag => { return paginate(files, { params: { tag }, // no longer hardcoded 🎉 })})Step 2: Give each tag only its own posts
Right now every tag paginates all files. We need to filter down to the posts that actually have this tag. We can do this with a simple filter function.
return uniqueTags.flatMap(tag => { const taggedPosts = files.filter(post => post.data.tags?.includes(tag)) return paginate(taggedPosts, { params: { tag }, })})Step 3: Slugify tags
The last step is to ensure that each tag is a valid slug. One way to do that is to convert them into kebab case. You can use the toKebab function in Splendid utils to handle this job.
While we do this, we pass the original tag name into props.
import { toKebab } from '@splendidlabz/utils'
export async function getStaticPaths({ paginate }) { // ... return uniqueTags.flatMap(tag => { const taggedPosts = files.filter(post => post.data.tags?.includes(tag)) return paginate(taggedPosts, { params: { tag: toKebab(tag) }, props: { tagName: tag }, }) })}Putting everything together
When we put everything together, the code for generating tag pages looks like this:
import { getCollection } from 'astro:content'import { toKebab } from '@splendidlabz/utils'
export async function getStaticPaths({ paginate }) { const blog = await getCollection('blog') const posts = await processFiles(blog) const tags = [...new Set(posts.flatMap(post => post.data.tags || []))] return tags.flatMap(tag => { const taggedPosts = posts.filter(post => post.data.tags?.includes(tag)) return paginate(taggedPosts, { params: { tag: toKebab(tag) }, props: { tagName: tag }, }) })}The easy way
The code doesn’t look long, but it is quite complicated. Instead of having to remember what’s going on every single time, I chose to put this into a function called gatherTagPages.
Then we can use it like this.
import { gatherTagPages } from '@splendidlabz/astro/content'export async function getStaticPaths({ paginate }) { const blog = await getCollection('blog') const posts = await processFiles(blog) return gatherTagPages(posts, paginate)}Much easier.
We’re finally done with content collections. Now let’s talk about the 3rd way to use Markdown — markdown layouts.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson