Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core/queriesObserver): pass query hashes when combining in … #12633

Mergedyashhbeladiya(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/12633/headmerged 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. πŸ¦‹ 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
      NameType
      @tanstack/query-corePatch
      @tanstack/angular-queryPatch
      @tanstack/lit-queryPatch
      @tanstack/preact-queryPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-devtoolsPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/react-queryPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/angular-query-devtoolsPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/preact-query-devtoolsPatch
      @tanstack/preact-query-persist-clientPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/eslint-plugin-queryPatch

      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

    2. Just noticed #12632 was opened a minute before this one with the same fix in #notify. Only the tests differ. Happy for you to go with whichever you prefer, and I'll close mine if you pick #12632.

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change β†’

      No actionable comments were generated in the recent review.

      Walkthrough

      QueriesObserver.#notify now passes the matched queries’ hashes to #combineResult. A regression test checks the combined result when an existing query resolves before setQueries receives 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
      #notify passes 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 #combineResult to address the same stale combined result.

      Suggested reviewers: sukvvon, tkdodo

      Merge Risk: Minimal Β· up to aab2d

      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: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 checkPassedIssue #12626 requires that the combined result of useQueries with a stable combine reflect the queries of the current render. The root cause is that #notify calls #combineResult without queryHashes, s…
      Out of Scope Changes checkPassedThe 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 #12626 requires that the combined result of useQueries with a stable combine reflect the queries of the current render. The root cause is that #notify calls #combineResult without queryHashes, so #lastQueryHashes keeps the new list while the cached result holds the old list. At the reviewed head, queriesObserver.ts #notify passes 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.

      Share

      Recent review info

      Run configuration

      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.md
      • packages/query-core/src/__tests__/queriesObserver.test.tsx
      • packages/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.

    4. tanstack-source added labelsbug

    5. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution β†— for commit aab2d96e3a90f8f7515400fe3a86f9da40eb7107

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...βœ… Succeeded8m 40sView β†—
      nx run-many --target=build --exclude=examples/*...βœ… Succeeded30sView β†—

      ☁️ Nx Cloud last updated this comment at 2026-11-22 18:50:02 UTC

    6. More templates

      @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@12633

      commit: aab2d96

    7. TkDodo merged this7843249

    8. TkDodo closed this

Sign in to join this conversation.