Skip to content

Conversation

@melvinhagberg
Copy link

The initial query was fetching exactly pageSize items, but the peek-ahead logic requires pageSize + 1 to determine if more pages exist. This mismatch caused hasNextPage to incorrectly return false on initial load when using SQLite predicate push-down with syncMode: "on-demand".

The issue occurred because:

  1. Initial query loaded exactly pageSize items (e.g., 50)
  2. setWindow({ limit: pageSize + 1 }) tried to access the peek-ahead item
  3. The item didn't exist yet, and lazy loading had timing issues
  4. hasNextPage was incorrectly false even when more data existed

The fix aligns the initial query limit with what setWindow expects, fetching pageSize + 1 items upfront. The extra item is only used internally for the hasNextPage check - the returned data is still sliced to pageSize items via the existing useMemo logic.

🎯 Changes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@melvinhagberg melvinhagberg marked this pull request as draft January 30, 2026 09:31
…l query

The initial query was fetching exactly `pageSize` items, but the
peek-ahead logic requires `pageSize + 1` to determine if more pages
exist. This mismatch caused `hasNextPage` to incorrectly return `false`
on initial load when using SQLite predicate push-down with
`syncMode: "on-demand"`.

The issue occurred because:
1. Initial query loaded exactly `pageSize` items (e.g., 50)
2. `setWindow({ limit: pageSize + 1 })` tried to access the peek-ahead item
3. The item didn't exist yet, and lazy loading had timing issues
4. `hasNextPage` was incorrectly `false` even when more data existed

The fix aligns the initial query limit with what `setWindow` expects,
fetching `pageSize + 1` items upfront. The extra item is only used
internally for the `hasNextPage` check - the returned `data` is still
sliced to `pageSize` items via the existing useMemo logic.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@melvinhagberg melvinhagberg force-pushed the fix/infinite-query-peek-ahead-initial-load branch from 6b8fded to a78ec9e Compare January 30, 2026 09:32
@changeset-bot
Copy link

changeset-bot bot commented Jan 30, 2026

🦋 Changeset detected

Latest commit: a78ec9e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/react-db Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

1 participant