`useLiveQuery` pagination breaks on revisited pages when `syncMode='on-demand'` #1434
Send to my agent- I've validated the bug against the latest version of DB packages
"dependencies": {
"@tanstack/db": "^0.6.1",
"@tanstack/query-core": "^5.95.2",
"@tanstack/query-db-collection": "^1.0.32",
"@tanstack/react-db": "^0.1.79",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"react": "^19.2.4",
"react-dom": "^19.2.4",
"typescript": "^6.0.2",
"vite": "^6.3.5"
}
Describe the bug
When using useLiveQuery with a paginated syncMode: 'on-demand' collection, navigating back to a previously-visited page causes subsequent page navigations throw errors.
Eager collections work fine.
To Reproduce Steps to reproduce the behavior:
- Go to https://stackblitz.com/edit/vitejs-vite-c5zum7r4?file=src%2FBugDemo.tsx
- Follow steps on demo page.
Expected behavior Navigating back to a previously-visited page should load its rows correctly, and subsequent navigations should continue to work normally.
Additional context On the demo, after page load: Clicking Page 2: No error. Clicking Page 3: No error. Clicking Page 2: Collection "live-query-4" sync cleanup function threw an error: Ordered snapshot was requested but no index was found. You have to call setOrderByIndex before requesting an ordered snapshot. Clicking Page 4: Invalid collection status transition from "cleaned-up" to "ready" for collection "live-query-4" Clicking Page 5: No error thrown, but no data either. Clicking Page 4: Collection "live-query-7" sync cleanup function threw an error: Invalid collection status transition from "cleaned-up" to "ready" for collection "live-query-4", but data loads now. Clicking Page 5: Three errors, all: Invalid collection status transition from "cleaned-up" to "ready" for collection "live-query-4"
Thank you for all your hard work with this awesome library :)
samwillis(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Hi @thebigthing313,
It appears that an index is missing, hence, it's throwing this error because it can't load the necessary data from the index. Indexes used to be created automatically, but we changed this in v0.6 and now the default is to not create any indexes unless you opt into auto indexing.
In your collection configuration, in the
QueryCollectionOptionsif you add these 2 lines it works:While this fixes your problem, applications shouldn't crash because they are configured in on-demand mode without opting into auto indexing. I'll investigate the issue to see how we should solve it (either not rely on an index or throw an error when creating the collection in on-demand mode).