fix(query-core/queriesObserver): pass query hashes when combining in β¦ #12633
main fromrefs/pull/12633/headΒ·merged by TkDodo View on GitHubβ¦'#notify' so a stale combined result is not returned (#12626)
π― Changes#
Fixes #12626
useQueries with a stable combine could hand back a combined result built from the previous query list. If a query from the old list updated between the render and the effect that calls setQueries, #notify re-ran combine on the old results but didn't pass any query hashes. So #lastQueryHashes still held the new list from the render. On the next render the hashes looked unchanged, #result === #lastResult, and the old (shorter) combined result was returned. That's how the reporter ended up with results[index] being undefined.
The fix is a one-liner: #notify now passes the hashes of the queries it's actually combining (from #observerMatches). The next render sees that the list changed and recomputes. It doesn't add extra combine calls, because #result has always just changed when #notify runs.
I added a test in queriesObserver.test.tsx that follows the same order as useQueries: subscribe with one query, render with two, let the first query resolve before setQueries runs, then render again. It fails on main (gets 1 result instead of 2) and passes with the fix. The useQueries tests in react, preact, vue, solid, svelte and lit still pass.
About test:pr: everything passes except the Next.js example and integration builds. Those fail the same way on main, because Next.js 16.4.4 refuses to build over a known security advisory (SecurityFatalError). That's unrelated to this change (the renovate PR #12558 bumps Next).
β Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.
π Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Fixed an issue where combined query results could become stale when an existing query updated while the observed query list was changing. Combined results now continue to reflect the current set of queries, including newly added queries, before and after an existing query finishes updating.
- 1
No actionable comments were generated in the recent review.
Walkthrough
-
QueriesObserver.#notifynow passes the matched queriesβ hashes to#combineResult. A regression test checks the combined result when an existing query resolves beforesetQueriesreceives an expanded query list.Changes: QueriesObserver combined results
Layer / File(s) Summary Notification hash handling
packages/query-core/src/queriesObserver.ts,packages/query-core/src/__tests__/queriesObserver.test.tsx,.changeset/fix-queries-observer-stale-combine.md#notifypasses the matched query hashes to#combineResult. A regression test checks that the combined result still contains two queries after an existing query resolves. The changeset declares a patch for@tanstack/query-core.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix Β· Severity of issue fixed: Medium
Possibly related PRs
- TanStack/query#12632: Passes the notifying observer listβs hashes to
#combineResultto address the same stale combined result.
Suggested reviewers:
sukvvon,tkdodo - TanStack/query#12632: Passes the notifying observer listβs hashes to
Merge Risk:
Minimal Β· up toaab2d-
This fix makes useQueries with a stable combine reflect the current query list instead of a stale shorter result. It is a small change with a regression test and no identified merge-blocking risk.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the fix in QueriesObserver (passing query hashes when combining), which matches the changed code in queriesObserver.ts. It is specific enough for a teammate to see the main change. It β¦ Description check The description follows the repository template. It has the Changes section with a clear explanation of the root cause, the one-line fix, and a regression test. The checklist is filled in and the releβ¦ Linked Issues check Issue #12626requires that the combined result of useQueries with a stable combine reflect the queries of the current render. The root cause is that#notifycalls#combineResultwithout queryHashes, sβ¦Out of Scope Changes check The whole-PR diff against the merge base contains three changes: a three-line change in packages/query-core/src/queriesObserver.ts that passes query hashes in #notify, a regression test in packages/quβ¦Full details: Title check
-
Explanation
The title names the fix in QueriesObserver (passing query hashes when combining), which matches the changed code in queriesObserver.ts. It is specific enough for a teammate to see the main change. It is truncated with a trailing 'in β¦', which weakens clarity but does not make it misleading.
Full details: Description check
-
Explanation
The description follows the repository template. It has the Changes section with a clear explanation of the root cause, the one-line fix, and a regression test. The checklist is filled in and the release impact is marked. It also reports test results and the unrelated Next.js build failure. The description starts with a stray fragment of the commit-style title ('β¦'
#notify' so a stale combined result...'), which is a minor formatting issue.
Full details: Linked Issues check
-
Explanation
Issue
#12626requires that the combined result of useQueries with a stable combine reflect the queries of the current render. The root cause is that#notifycalls#combineResultwithout queryHashes, so#lastQueryHasheskeeps the new list while the cached result holds the old list. At the reviewed head, queriesObserver.ts#notifypasses queryHashes built from this.#observerMatches (match.defaultedQueryOptions.queryHash) to#combineResult. The next render therefore detects the list change and recomputes. This matches the fix proposed in the issue. A regression test in queriesObserver.test.tsx reproduces the issue sequence: subscribe with one query, getOptimisticResult with two queries, let the existing query resolve, then re-render, and expects a count of 2 both times. The test is automated and covers the coding requirement. Tests were not executed in this review; the result relies on the diff and the PR's reported test runs.
Full details: Out of Scope Changes check
-
Explanation
The whole-PR diff against the merge base contains three changes: a three-line change in packages/query-core/src/queriesObserver.ts that passes query hashes in
#notify, a regression test in packages/query-core/src/tests/queriesObserver.test.tsx for the same bug, and a patch changeset for@tanstack/query-core. Each change connects directly to#12626. The changeset is the release artifact the PR description says is required for published code. No unrelated changes were found.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/query
- Branch: main
- File: .coderabbit.yaml
- Commit: 816a0aa
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
afddd6c1-39a0-4bad-8d2b-be697e48c1b9
Commits-
Reviewing files that changed from the base of the PR and between 816a0aab2e904cd5364d3b0f278582eccc660318 and aab2d96e3a90f8f7515400fe3a86f9da40eb7107.
Files selected for processing: 3-
.changeset/fix-queries-observer-stale-combine.mdpackages/query-core/src/__tests__/queriesObserver.test.tsxpackages/query-core/src/queriesObserver.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
yashhbeladiya(GitHub profile, opens in a new tab) linked this to fix #12626
tanstack-source added labelsbug
View your CI Pipeline Execution β for commit aab2d96e3a90f8f7515400fe3a86f9da40eb7107
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...β Succeeded 8m 40s View β nx run-many --target=build --exclude=examples/*...β Succeeded 30s View β
βοΈ Nx Cloud last updated this comment at
2026-11-22 18:50:02UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-angular-router
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel-angular-router
- @tanstack/query-example-angular-dynamic-devtools
- @tanstack/query-example-angular-dynamic-queries
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-angular-ssr
- @tanstack/query-example-angular-ssr-persist
- @tanstack/query-example-angular-to-resource-signal-forms
- @tanstack/query-example-lit-algolia
- @tanstack/query-example-lit-auto-refetching
- @tanstack/query-example-lit-basic
- @tanstack/query-example-lit-basic-graphql-request
- @tanstack/query-example-lit-batching
- @tanstack/query-example-lit-chat
- @tanstack/query-example-lit-default-query-function
- @tanstack/query-example-lit-infinite-query-with-max-pages
- @tanstack/query-example-lit-load-more-infinite-scroll-lit-labs-router
- @tanstack/query-example-lit-offline-lit-labs-router
- @tanstack/query-example-lit-optimistic-updates
- @tanstack/query-example-lit-pagination
- @tanstack/query-example-lit-playground
- @tanstack/query-example-lit-prefetching-lit-labs-router
- @tanstack/query-example-lit-rick-morty-lit-labs-router
- @tanstack/query-example-lit-simple
- @tanstack/query-example-lit-ssr
- @tanstack/query-example-lit-star-wars-lit-labs-router
- @tanstack/query-example-preact-algolia
- @tanstack/query-example-preact-auto-refetching
- @tanstack/query-example-preact-basic
- @tanstack/query-example-preact-basic-graphql-request
- @tanstack/query-example-preact-batching
- @tanstack/query-example-preact-chat
- @tanstack/query-example-preact-default-query-function
- @tanstack/query-example-preact-devtools-panel
- @tanstack/query-example-preact-infinite-query-with-max-pages
- @tanstack/query-example-preact-load-more-infinite-scroll-preact-iso
- @tanstack/query-example-preact-offline-preact-iso
- @tanstack/query-example-preact-optimistic-updates
- @tanstack/query-example-preact-pagination
- @tanstack/query-example-preact-playground
- @tanstack/query-example-preact-prefetching-preact-iso
- @tanstack/query-example-preact-rick-morty-preact-iso
- @tanstack/query-example-preact-shadow-dom
- @tanstack/query-example-preact-simple
- @tanstack/query-example-preact-ssr
- @tanstack/query-example-preact-ssr-streaming
- @tanstack/query-example-preact-star-wars-preact-iso
- @tanstack/query-example-preact-suspense
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching-nextjs-pages-router
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-react-batching
- @tanstack/query-example-react-chat
- @tanstack/query-example-react-contacts-react-router
- @tanstack/query-example-react-contacts-tanstack-router
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-react-eslint-legacy
- @tanstack/query-example-react-eslint-plugin-demo
- @tanstack/query-example-react-hydration-nextjs-pages-router
- @tanstack/query-example-react-infinite-query-with-max-pages-nextjs-pages-router
- @tanstack/query-example-react-load-more-infinite-scroll-nextjs-pages-router
- @tanstack/query-example-react-offline-react-router
- @tanstack/query-example-react-offline-tanstack-router
- @tanstack/query-example-react-optimistic-updates-nextjs-app-router
- @tanstack/query-example-react-pagination-nextjs-pages-router
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching-nextjs-app-router
- @tanstack/query-example-react-prefetching-nextjs-pages-router
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-rick-morty-react-router
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars-react-router
- @tanstack/query-example-react-suspense
- @tanstack/query-example-react-suspense-streaming-nextjs-app-router
- @tanstack/query-example-solid-algolia
- @tanstack/query-example-solid-auto-refetching-solid-start2
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-batching
- @tanstack/query-example-solid-chat
- @tanstack/query-example-solid-contacts-solid-router
- @tanstack/query-example-solid-contacts-tanstack-router
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-devtools-panel
- @tanstack/query-example-solid-offline-solid-router
- @tanstack/query-example-solid-offline-tanstack-router
- @tanstack/query-example-solid-optimistic-updates-solid-start2
- @tanstack/query-example-solid-pagination-solid-start2
- @tanstack/query-example-solid-playground
- @tanstack/query-example-solid-prefetching-solid-start2
- @tanstack/query-example-solid-rick-morty-solid-router
- @tanstack/query-example-solid-shadow-dom
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-ssr-astro
- @tanstack/query-example-solid-star-wars-solid-router
- @tanstack/query-example-solid-streaming-solid-start2
- @tanstack/query-example-solid-suspense
- @tanstack/query-example-svelte-algolia
- @tanstack/query-example-svelte-auto-refetching-sveltekit
- @tanstack/query-example-svelte-basic-graphql-request
- @tanstack/query-example-svelte-basic-sveltekit
- @tanstack/query-example-svelte-batching
- @tanstack/query-example-svelte-chat
- @tanstack/query-example-svelte-contacts-sveltekit
- @tanstack/query-example-svelte-default-query-function
- @tanstack/query-example-svelte-infinite-query-with-max-pages-sveltekit
- @tanstack/query-example-svelte-load-more-infinite-scroll-sveltekit
- @tanstack/query-example-svelte-offline-sveltekit
- @tanstack/query-example-svelte-optimistic-updates-sveltekit
- @tanstack/query-example-svelte-pagination-sveltekit
- @tanstack/query-example-svelte-playground-sveltekit
- @tanstack/query-example-svelte-prefetching-sveltekit
- @tanstack/query-example-svelte-rick-morty-sveltekit
- @tanstack/query-example-svelte-shadow-dom
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr-sveltekit
- @tanstack/query-example-svelte-star-wars-sveltekit
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-algolia
- @tanstack/query-example-vue-auto-refetching-nuxt4
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-basic-graphql-request
- @tanstack/query-example-vue-batching
- @tanstack/query-example-vue-chat
- @tanstack/query-example-vue-contacts-tanstack-router
- @tanstack/query-example-vue-contacts-vue-router
- @tanstack/query-example-vue-default-query-function
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-devtools-panel
- @tanstack/query-example-vue-infinite-query-with-max-pages-nuxt4
- @tanstack/query-example-vue-load-more-infinite-scroll-nuxt4
- @tanstack/query-example-vue-offline-tanstack-router
- @tanstack/query-example-vue-offline-vue-router
- @tanstack/query-example-vue-optimistic-updates-nuxt4
- @tanstack/query-example-vue-pagination-nuxt4
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-playground
- @tanstack/query-example-vue-prefetching-nuxt4
- @tanstack/query-example-vue-rick-morty-vue-router
- @tanstack/query-example-vue-shadow-dom
- @tanstack/query-example-vue-simple
- @tanstack/query-example-vue-simple-nuxt3
- @tanstack/query-example-vue-simple-nuxt4
- @tanstack/query-example-vue-star-wars-vue-router
- @tanstack/query-example-vue-suspense
@tanstack/angular-query
npm i https://pkg.pr.new/@tanstack/angular-query@12633@tanstack/angular-query-devtools
npm i https://pkg.pr.new/@tanstack/angular-query-devtools@12633@tanstack/angular-query-persist-client
npm i https://pkg.pr.new/@tanstack/angular-query-persist-client@12633@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@12633@tanstack/lit-query
npm i https://pkg.pr.new/@tanstack/lit-query@12633@tanstack/preact-query
npm i https://pkg.pr.new/@tanstack/preact-query@12633@tanstack/preact-query-devtools
npm i https://pkg.pr.new/@tanstack/preact-query-devtools@12633@tanstack/preact-query-persist-client
npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@12633@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@12633@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@12633@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@12633@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@12633@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@12633@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@12633@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@12633@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@12633@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@12633@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@12633@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@12633@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@12633@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@12633@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@12633@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@12633@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@12633@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@12633@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@12633commit:
aab2d96TkDodo merged this7843249
TkDodo closed this
Sign in to join this conversation.
π¦ Changeset detected#
Latest commit: aab2d96e3a90f8f7515400fe3a86f9da40eb7107
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 26 packages
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