AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Blog Category Filter Is Lost When Loading More Posts

When a blog category filter is active, paginated requests to /api-v2/blog-posts omit the category parameter, causing unfiltered posts to be appended to the list.

mediumConfidence 95%Next.js

Origin Analysis

In fetchMorePosts(), the code only includes filterParams.search, but not filterParams.category. The category is stored in filterParams by handleFilterChange, but not propagated to subsequent paginated requests.
1. Open the blog page. 2. Select a category with more than 25 posts (e.g., postgres). 3. Scroll to bottom to load more posts. 4. Observe network request for second page lacks category parameter. 5. See posts from other categories appended.

Fixing Code Block

const fetchMorePosts = useCallback(async () => { if (loading || !hasMore) return; setLoading(true); const params = new URLSearchParams({ offset: String(posts.length), limit: '25', }); if (filterParams.search) { params.set('search', filterParams.search); } // Preserve category filter on pagination if (filterParams.category && filterParams.category !== 'all') { params.set('category', filterParams.category); } try { const response = await fetch(`/api-v2/blog-posts?${params.toString()}`); const data = await response.json(); setPosts((prev) => [...prev, ...data.posts]); setHasMore(data.hasMore); } catch (error) { console.error('Failed to fetch more posts:', error); } finally { setLoading(false); } }, [loading, hasMore, posts.length, filterParams.search, filterParams.category]);
Adds condition to include filterParams.category in query params when it is not 'all', ensuring paginated requests carry the selected category filter.

Edge Case Audit

Potential edge cases: if category value is not URL-safe, it should be URL-encoded; URLSearchParams handles encoding when using set. Ensure that filterParams.category is a string (not object) and consistent with the API's expected category parameter. Rollback: remove the added if block and revert to previous behavior; however, the bug will persist. Also verify that the category state is cleared appropriately when user selects 'all' or resets filters.

Ecosystem Topology