fix(blog): return results newest first#7898
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughThe PR extends the blog search API to include date-based sorting and deduplication. The ChangesBlog Search Date Sorting and Deduplication
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/blog/src/app/api/search/route.ts`:
- Around line 41-43: The sort comparator is calling localeCompare on
metadata.date which may be undefined; update the comparator and any direct date
comparisons (and the other comparisons at the same spots) to guard and normalize
the value first — e.g., compute safeDateA = String(a.generated_metadata?.date ??
"") and safeDateB = String(b.generated_metadata?.date ?? "") (or use
metadata?.date ?? "" when you already have metadata variable) and call
safeDateA.localeCompare(safeDateB); similarly ensure any other uses of
metadata.date (and other loosely-typed generated_metadata fields referenced
around the same code) are null-coalesced to a safe default before calling string
methods.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 3ff9d2e1-daaf-40ec-b01a-899fd2539056
📒 Files selected for processing (2)
apps/blog/src/app/api/search/route.tsapps/blog/src/lib/search-types.ts
| const metadata = item.generated_metadata as unknown as GeneratedMetadata; | ||
| const slug = (metadata?.slug ?? "").replace(/^\/+/, ""); | ||
| const title = metadata?.metaTitle ?? metadata?.title ?? "Untitled"; |
There was a problem hiding this comment.
Guard date before sorting to avoid runtime crashes.
Because generated_metadata is loosely typed, metadata.date can be missing; then Line 63 can throw when calling localeCompare on undefined.
Proposed fix
- const metadata = item.generated_metadata as unknown as GeneratedMetadata;
+ const metadata = item.generated_metadata as Partial<GeneratedMetadata> | undefined;
const slug = (metadata?.slug ?? "").replace(/^\/+/, "");
const title = metadata?.metaTitle ?? metadata?.title ?? "Untitled";
+ const date = typeof metadata?.date === "string" ? metadata.date : "";
const formattedUrl = slug ? `/${slug}` : "#";
const base = `${item.file_id}-${item.chunk_index}`;
@@
- date: metadata.date,
+ date,
},
];
@@
- .sort((a, b) => b.date.localeCompare(a.date))
+ .sort((a, b) => b.date.localeCompare(a.date))Also applies to: 56-57, 63-63
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/blog/src/app/api/search/route.ts` around lines 41 - 43, The sort
comparator is calling localeCompare on metadata.date which may be undefined;
update the comparator and any direct date comparisons (and the other comparisons
at the same spots) to guard and normalize the value first — e.g., compute
safeDateA = String(a.generated_metadata?.date ?? "") and safeDateB =
String(b.generated_metadata?.date ?? "") (or use metadata?.date ?? "" when you
already have metadata variable) and call safeDateA.localeCompare(safeDateB);
similarly ensure any other uses of metadata.date (and other loosely-typed
generated_metadata fields referenced around the same code) are null-coalesced to
a safe default before calling string methods.
|
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
Summary by CodeRabbit
Bug Fixes
New Features