Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Paginated tag pages

3m:11s

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:

Terminal window
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:

  1. The [tag] value
  2. 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.