diff --git a/.changeset/fix-infinite-query-peek-ahead.md b/.changeset/fix-infinite-query-peek-ahead.md new file mode 100644 index 000000000..1a6811e82 --- /dev/null +++ b/.changeset/fix-infinite-query-peek-ahead.md @@ -0,0 +1,7 @@ +--- +"@tanstack/react-db": patch +--- + +fix(react-db): include peek-ahead item in useLiveInfiniteQuery initial query + +The initial query was fetching exactly `pageSize` items, but the peek-ahead logic requires `pageSize + 1` to determine if more pages exist. This caused `hasNextPage` to incorrectly return `false` on initial load when using SQLite predicate push-down with `syncMode: "on-demand"`. diff --git a/packages/react-db/src/useLiveInfiniteQuery.ts b/packages/react-db/src/useLiveInfiniteQuery.ts index 405ecb583..3f4d74705 100644 --- a/packages/react-db/src/useLiveInfiniteQuery.ts +++ b/packages/react-db/src/useLiveInfiniteQuery.ts @@ -184,10 +184,11 @@ export function useLiveInfiniteQuery( // Create a live query with initial limit and offset // Either pass collection directly or wrap query function + // Use pageSize + 1 for the initial limit to include the peek-ahead item const queryResult = isCollection ? useLiveQuery(queryFnOrCollection) : useLiveQuery( - (q) => queryFnOrCollection(q).limit(pageSize).offset(0), + (q) => queryFnOrCollection(q).limit(pageSize + 1).offset(0), deps, )