Development

Tag Filtering - agiusalexandre.com

Blog readers needed a way to browse content by topic instead of scrolling through all posts.

Alexandre Agius

Alexandre Agius

AWS Solutions Architect

2 min read
Share:

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 /tags showing 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

FileChange
src/pages/tags/index.astroNew tags index page with post counts
src/pages/tags/[tag].astroDynamic tag filter pages
src/components/PostCard.astroMade tags clickable with color styling

What I Learned

  • Astro’s content collections make tag aggregation straightforward — The getCollection API 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

Alexandre Agius

Alexandre Agius

AWS Solutions Architect

Passionate about AI & Security. Building scalable cloud solutions and helping organizations leverage AWS services to innovate faster. Specialized in Generative AI, serverless architectures, and security best practices.

Related Posts

Back to Blog