How My Related Posts Component Works (Zero Manual Maintenance, Driven by Tags)
Why I replaced manual cross-links with a tag-scored RelatedPosts component in Next.js 16—and the exact ranking logic behind it.

Once you have more than two or three posts on a blog, you want readers to keep clicking to related articles at the bottom of the page. 🔗
The naive way to do that is adding a manual "Related Posts" bullet list at the bottom of every Markdown file. That works for a week—and then it turns into a maintenance chore. Every time you publish a new post, you have to go back and edit five older posts just to link to it, and if you accidentally link to an unpublished draft, readers hit a 404.
Instead, reggiepangilinan.com renders a dedicated RelatedPosts component automatically at the bottom of every article. No manual lists, no broken draft links, and no extra infrastructure.
🧠 1. Exactly How It Decides What's Related
All of the ranking logic lives in one small server-side helper: getRelatedBlogPosts() in src/lib/contentful.ts.
Here is the exact four-step pipeline it runs for a given post:
Fetch & exclude the current post: It calls
getBlogPosts(50, 0, options)—which already returns posts pre-sorted newest-first by['-fields.publishDate', '-sys.createdAt']—and filters out the article you're currently reading (fields.slug !== currentSlug).Count shared tags (case-insensitive): It puts the current post's
tagsinto a lowercaseSetand counts how many tags each candidate post shares (sharedCount).Prefer tag matches, fall back to latest: If at least one candidate has
sharedCount > 0, it filters down to only those matching posts. If you publish a brand-new topic with zero overlapping tags yet, it gracefully falls back to the most recent posts instead of showing an empty gap.Rank by overlap, tie-break by recency: It sorts by
b.sharedCount - a.sharedCountand takes the top 3. Because JavaScript'sArray.prototype.sort()is stable, any two posts with the same number of shared tags automatically keep their original newest-first order (publishDate, thencreatedAt).

⚡ 2. Zero Waterfall (and Cached by ISR)
Adding a second Contentful query on a blog post page shouldn't slow down page loads.
Inside src/app/blog/[slug]/page.tsx, getRelatedBlogPosts() runs inside Promise.all right alongside resolveRichTextAssets()—so both resolve concurrently with zero extra sequential latency.
Even better:
24-hour ISR caching: On normal visits, the rendered related cards are cached as part of the post's 24-hour ISR HTML—zero runtime CMS calls for readers.
Draft Preview aware: It forwards
{ preview: isDraftMode }, so when you're previewing an unpublished post in Contentful Studio, it stays consistent with Preview Mode without ever leaking drafts onto the live site.
🎨 3. The UI Component & Graceful Degradation
The UI itself in src/components/RelatedPosts/RelatedPosts.tsx and RelatedPosts.module.css stays intentionally lightweight:
Hides cleanly when empty: If there are no other posts yet (or during a zero-secret local build), it returns
nullrather than rendering an empty section header.Responsive auto-fit grid: Uses
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr))so 1, 2, or 3 cards always balance naturally across mobile and desktop.Scannable cards: Each card shows the formatted publish date, title, a 3-line clamped excerpt, and up to 2 tags—all styled with the site's global CSS custom properties in
src/app/globals.css.
🏷️ 4. What Actually Drives Quality: Tag Discipline
You don't need embeddings or a recommendation engine for a personal blog—you just need consistent tags.
Because the scoring is 100% driven by frontmatter tags, the real secret is reusing a small, steady vocabulary across posts (Next.js, Contentful, Netlify, TypeScript, GitHub Actions, Security, SEO, UX) instead of inventing one-off tags like nextjs-16-app-router-tips. I documented that rule right in AGENTS.md so every new draft—whether written by me or with a coding agent—automatically plugs into the right cluster of related posts.
✨ Wrapping Up
A 30-line tag-scoring helper + a clean server component turned cross-linking from a manual chore into something that just works every time I hit Publish. As new posts go live, older articles naturally start recommending them without a single manual edit.
You can inspect the full implementation in src/lib/contentful.ts and src/components/RelatedPosts/RelatedPosts.tsx on GitHub.
Related Posts
Swiss Design Meets Punk Rock: How I Built an Automated Hero Poster System for My Blog
A plain-English look at the HTML/SVG poster generator behind my blog hero images, a quick intro to the Swiss International Style and punk-zine energy that inspired it, and how every poster flows from Git to Contentful.
Auto-Embedding GitHub Gists in Contentful Rich Text with Lazy-Loaded PrismJS
How RichTextRenderer detects standalone GitHub Gist URLs in Contentful Rich Text and renders them via a custom GitHubGist component with dynamically imported PrismJS grammars.
Client-Side QR Code Sharing and Smart Open Graph Cards in Next.js
How I built PostShareBar (copy link, Web Share API, and downloadable client-side QR codes) alongside word-boundary SEO truncation and auto-cropped 1200×630 Open Graph cards.