Skip to content

Commit c307b47

Browse files
decobotclaude
andcommitted
fix(blog): drop posts without a slug from listings
A post whose slug is missing, empty or blank has no route. It still rendered in listings, producing cards that link nowhere and a broken url in the JSON-LD. Filter it out in filterPosts, ahead of every other filter and of slicePosts, so `count` still yields `count` renderable posts. Records come straight from the CMS and are cast without validation, so the guard also checks the type: a non-string slug would otherwise throw inside handlePosts, and the try/catch in the loaders would turn that into an empty listing. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b5abb45 commit c307b47

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

blog/core/handlePosts.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,25 @@ export const slicePosts = (
169169
return posts.slice(startIndex, endIndex);
170170
};
171171

172+
/**
173+
* A record without a slug has no route, so it can never be rendered: listing it
174+
* only produces cards linking to the listing itself. Dropped here, before
175+
* slicePosts, so `count` still yields `count` renderable posts.
176+
*/
177+
export const filterRoutablePosts = (posts: BlogPost[]) =>
178+
// Records come straight from the CMS, so `slug` is only a string by
179+
// convention: the typeof guard keeps a malformed one from throwing here and
180+
// taking the whole listing down with it.
181+
posts.filter(({ slug }) => typeof slug === "string" && slug.trim());
182+
172183
const filterPosts = (
173-
posts: BlogPost[],
184+
allPosts: BlogPost[],
174185
slug?: string | string[],
175186
postSlugs?: string[],
176187
term?: string,
177188
): BlogPost[] => {
189+
const posts = filterRoutablePosts(allPosts);
190+
178191
if (typeof slug === "string") {
179192
const firstFilter = postSlugs && postSlugs.length > 0
180193
? filterPostsBySlugs(posts, postSlugs)

0 commit comments

Comments
 (0)