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

Better tag pages

2m:34s

I think of tag pages as wonderful archives for knowing what an author writes about — so they should include the number of articles in each tag. Otherwise, it is difficult for people to know how many articles there are.

Here’s an example:

Gathering tags

To make this happen, we need to gather the tags into an array that contains two things — the tags themselves, and the number of items within each tag.

const tags = [
{ name: 'astro', items: 18 },
{ name: 'css', items: 20 }
]

The only way to create this tags array is to loop through the content collection, look at the frontmatter for each post, and see what tags are available.

We can write a gatherTags function to do this.

---
import { getCollection } from 'astro:content'
const blog = await getCollection('blog')
const tags = gatherTags(blog)
// Gathers tags into a usable array
function gatherTags(files) {
// ...
}
---

Since we are looping through each post anyway, I think it’s even better to let the tags array have an array of posts within the tag. We can always use length to get the number of posts in the tag.

const tags = [
{
name: 'astro',
items: [{ /*...*/ }, { /*...*/ }, { /*...*/ }],
},
{
name: 'css',
items: [{ /*...*/ }, { /*...*/ }, { /*...*/ }],
},
]

With this in mind, I’ve created a gatherTags that looks like this:

// Gathers tags into a usable array
function gatherTags(files) {
const tags = {}
// Group files by tag
files.forEach(file => {
file.data.tags.forEach(tag => {
if (!tags[tag]) tags[tag] = []
tags[tag].push(file)
})
})
// Sort tags alphabetically and convert to an array for usage
return Object.keys(tags)
.sort()
.map(tag => ({ name: tag, items: tags[tag] }))
}

gatherTags is added to Splendid Astro so you don’t have to write it yourself. Just use it!

---
import { getCollection } from 'astro:content'
import { gatherTags } from '@splendidlabz/astro/content'
const blog = await getCollection('blog')
const tags = gatherTags(blog)
---

Use processFiles first

I recommend always using processFiles before gatherTags because it ensures the content folder is processed according to the changes we’ve made in blogs made easy.

---
// ...
const blog = await getCollection('blog')
const posts = await processFiles(blog)
const tags = gatherTags(posts)
---

Creating tag pages

Once we have gathered tags into tags, creating tag pages is remarkably simple — we’ll use getStaticPaths just like we’ve created blog pages in the previous lessons.

src/pages/tags/[tag].astro
---
export const prerender = true // optional if output is static
export async function getStaticPaths() {
const blog = await getCollection('blog')
const posts = processFiles(blog)
const tags = gatherTags(posts)
return tags.map(tag => ({
params: { tag: tag.name },
props: { posts: tag.items },
}))
}
const { tag } = Astro.params
const { posts } = Astro.props
---
<h1>{tag}</h1>
{
posts.map(post => (
<article>
<h2>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</h2>
<Markdown>{post.data.excerpt}</Markdown>
</article>
))
}