Tag Filtering - agiusalexandre.com
Blog readers needed a way to browse content by topic instead of scrolling through all posts.
The Problem
Blog readers had no way to browse content by topic. Finding articles about a specific subject meant scrolling through all posts hoping to spot relevant titles. With a growing number of articles, this was becoming frustrating.
The Solution
Built a complete tag browsing system:
- Tags index page at
/tagsshowing all tags with post counts - Individual tag pages at
/tags/[tag-name]listing filtered posts - Clickable tags throughout the site with consistent color coding
How It Works
The tag index page builds a count map and sorts tags by popularity:
// Build tag count map
const tagCounts = new Map<string, number>();
posts.forEach(post => {
post.data.tags?.forEach(tag => {
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
});
});
// Sort tags by count (descending), then alphabetically
const sortedTags = Array.from(tagCounts.entries())
.sort((a, b) => {
if (b[1] !== a[1]) return b[1] - a[1];
return a[0].localeCompare(b[0]);
});
Dynamic tag pages use Astro’s getStaticPaths to generate routes at build time:
export async function getStaticPaths() {
const posts = await getCollection('blog', ({ data }) => !data.draft);
// Collect all unique tags
const tags = new Set<string>();
posts.forEach(post => {
post.data.tags?.forEach(tag => tags.add(tag));
});
// Create a path for each tag
return Array.from(tags).map(tag => ({
params: { tag: tag.toLowerCase().replace(/\s+/g, '-') },
props: {
originalTag: tag,
posts: posts.filter(post => post.data.tags?.includes(tag))
},
}));
}
Color-coded tags maintain visual consistency:
const tagColors: Record<string, string> = {
'AWS': 'bg-orange-100 text-orange-700 hover:bg-orange-200',
'AI': 'bg-purple-100 text-purple-700 hover:bg-purple-200',
'Security': 'bg-red-100 text-red-700 hover:bg-red-200',
'Development': 'bg-blue-100 text-blue-700 hover:bg-blue-200',
'default': 'bg-gray-100 text-gray-700 hover:bg-gray-200',
};
Key Files Changed
| File | Change |
|---|---|
src/pages/tags/index.astro | New tags index page with post counts |
src/pages/tags/[tag].astro | Dynamic tag filter pages |
src/components/PostCard.astro | Made tags clickable with color styling |
What I Learned
- Astro’s content collections make tag aggregation straightforward — The
getCollectionAPI provides easy access to all posts and their metadata - Static generation handles tags naturally — No runtime database queries needed, all tag pages are pre-built
- URL slugification matters — Converting “AI-DLC” to “ai-dlc” ensures clean, consistent URLs while preserving the original tag name for display
What’s Next
- Add tag filtering to the main blog page (filter without navigating)
- Consider adding a tag cloud visualization
Related Posts
SEO Enhancement - agiusalexandre.com
Blog posts weren't appearing correctly in search results or showing rich previews when shared on social media.
DevelopmentBrowser Automation Agents - Amazon Bedrock AgentCore
Enterprise workflows often require interacting with web applications that lack APIs. Traditional automation scripts are brittle and break when UIs change.
DevelopmentAWS Backup Cost Analysis
EBS snapshot costs were growing month-over-month with no clear explanation or optimization strategy.
