Why does this cursor pagination helper repeat the last item on the next page?

sora
Mon, 06 Apr 2026 18:00:10 +0000

Hey everyone, I’m working on a small API helper for cursor pagination, and I’m trying to keep it simple without loading extra rows in memory. The problem is I either miss an item or repeat the last one from the previous page.

function getPage(items, cursor, limit) {
  let start = 0;

  if (cursor !== null) {
    start = items.findIndex(item => item.id >= cursor);
  }

  const page = items.slice(start, start + limit);
  const nextCursor = page.length ? page[page.length - 1].id : null;

  return { page, nextCursor };
}

What am I getting wrong with the cursor boundary here if I want stable pages without duplicates?

Sora