Skip to content

fix: handle posts without tags in related posts - #8001

Open
NSTKrishna wants to merge 1 commit into
layer5io:masterfrom
NSTKrishna:fix/related-posts-null-tags
Open

fix: handle posts without tags in related posts#8001
NSTKrishna wants to merge 1 commit into
layer5io:masterfrom
NSTKrishna:fix/related-posts-null-tags

Conversation

@NSTKrishna

@NSTKrishna NSTKrishna commented Aug 26, 2026

Copy link
Copy Markdown
Member

Description

This PR fixes #8000

Problem

When a blog post is authored without tags (or with tags: null / tags: []), RelatedPostsFactory encounters two issues:

  1. Runtime Crash on Candidate Posts: If any candidate post in the GraphQL query has frontmatter.tags as null or undefined, iterating over it with .forEach() throws a TypeError: Cannot read properties of null (reading 'forEach'), causing the related posts component to crash across all blog pages.
  2. Blocked Category Fallback: If the current blog post does not have tags, RelatedPostsFactory.getPosts() logged an error and exited early with return [], preventing the section from showing category-based recommendations (which carry +2 points per match).

Changes Made

  • Safeguard setTags(): Updated setTags(tags) to ensure this.tags always defaults to an array (Array.isArray(tags) ? tags : []), safely handling null and undefined.
  • Removed Early Exit Guard: Removed the strict if (!tags || tags.length === 0) guard in getPosts() so posts without tags can still recommend related posts based on matching categories.
  • Added Null Check in addTagsPoints(): Safely wrapped post.frontmatter.tags in addTagsPoints() with Array.isArray() before calling .forEach().

Notes for Reviewers

  • Tested with posts having:
    • Valid tags
    • Empty tags (tags: [])
    • tags: null / omitted tags
  • Verified that posts without tags now gracefully fall back to matching related posts by category without console errors or runtime crashes.

Signed commits

  • Yes, I signed my commits.

Signed-off-by: NSTKrishna <krishnagehlot936@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ffa9509-8e67-4e52-8a01-5de0764f86cb

📥 Commits

Reviewing files that changed from the base of the PR and between de916a9 and 63cbecc.

📒 Files selected for processing (1)
  • src/components/Related-Posts/relatedPostsFactory.js

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The related posts factory now normalizes invalid tag inputs to empty arrays. Related-post scoring no longer iterates directly over missing or non-array frontmatter tags.

Changes

Related post tag handling

Layer / File(s) Summary
Normalize tags before related-post scoring
src/components/Related-Posts/relatedPostsFactory.js
setTags accepts arrays and stores an empty array for other inputs. Tag scoring uses an empty array when candidate post tags are missing or invalid.

Estimated code review effort: 2 (Simple) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 63cbe

The PR safely handles posts with missing or null tags while preserving category-based recommendations; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #8000 by normalizing invalid current-post tags to an empty array and safely handling missing or non-array candidate-post tags. This preserves category-based recommendations a…
Out of Scope Changes check ✅ Passed The changes are limited to tag validation and safe related-post scoring in relatedPostsFactory.js. No unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: handling posts without tags in the related-posts logic.
Full details: Linked Issues check

Explanation

The changes address issue #8000 by normalizing invalid current-post tags to an empty array and safely handling missing or non-array candidate-post tags. This preserves category-based recommendations and prevents tag-scoring runtime errors.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1 files.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Preview deployment for PR #8001 removed.

This PR preview was automatically pruned because we keep only the 3 most recently updated previews on GitHub Pages to stay within deployment size limits.

If needed, push a new commit to this PR to generate a fresh preview.

@NSTKrishna
NSTKrishna requested a review from leecalcote August 27, 2026 05:05
@suryaff733

Copy link
Copy Markdown

@NSTKrishna LGTM 👍 Verified locally — fixes the null tag crash and category fallback works properly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RelatedPostsFactory throws runtime error / fails when blog posts have empty or null tags

2 participants