Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): keep combine hashes with notification results 🤖🤖🤖 #12632

Closedsakaintp(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12632/headopened View on GitHub

🎯 Changes

Fixes #12626.

An optimistic render can combine the next query list before the effect calls setQueries. If an old query then notifies, the observer combines its old results but leaves the new query hashes in the cache. The next render sees unchanged hashes and can return the old, shorter combined result.

This passes the notifying observer list's hashes into #combineResult, so the cached hashes always describe the results that were actually combined. The next optimistic render now detects the list change and recomputes.

The regression covers three queries growing to five, an update from the old list, and a re-render using trackResult. It fails on main with three results and passes with this change. React and Preact need no adapter-specific changes because they share this core behavior.

Size limit comparison: pending the PR's Preview CI; no local comparison is claimed.

✅ 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.

I did not run the full-repo test:pr on this 7.5 GiB host. The focused checks that passed are:

  • @tanstack/query-core:test:lib: 41 files, 1641 tests
  • @tanstack/react-query:test:lib: 37 files, 620 tests
  • @tanstack/preact-query:test:lib: 35 files, 570 tests
  • @tanstack/query-core:test:oxlint
  • @tanstack/query-core:test:types across the six configured TypeScript versions
  • @tanstack/query-core:build and test:build (publint and attw)
  • oxfmt --check and git diff --check

🚀 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 if an existing query updated after an optimistic query-list change but before that change was committed.
    • Combined results now reflect updates to existing queries during this transition, while continuing to include the full optimistic query list when read again.
    1. 🦋 Changeset detected

      Latest commit: 9026010c8a097062ac14518e17ea4b5a4b2e19bb

      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. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      When a query updates before setQueries commits a new query list, notifications now pass the current query hashes to result combination. A regression test checks that the listener receives updated data and that the optimistic result still contains five entries.

      Changes: Combined-result freshness

      Layer / File(s)Summary
      Pass current query hashes during notifications
      packages/query-core/src/queriesObserver.ts, packages/query-core/src/__tests__/queriesObserver.test.tsx, .changeset/calm-pandas-reflect.md
      #notify passes the ordered hashes from #observerMatches to #combineResult. The regression test checks notification data and optimistic result length when a query updates before setQueries commits the new list. The changeset declares a patch release for @tanstack/query-core.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Suggested reviewers: sukvvon, tkdodo

      Merge Risk: Minimal · up to 90260

      This fixes stale, shorter combined results in useQueries when an old query updates before the new query list is committed. The change is small and localized, and it ships with a regression test and a patch changeset, so merge risk is minimal.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the fix area (query-core) and the core change: keeping combine hashes aligned with notification results. It describes the main change in the changeset. The emoji noise (🤖🤖🤖) is extra t…
      Description checkPassedThe description follows the repository template. It has the Changes section with motivation, a linked issue (#12626), the checklist, and the Release Impact section with the changeset box checked. It e…
      Linked Issues checkPassedObjective [#12626]: a combined result must reflect the query list of the current render, even when an old query notifies before setQueries runs.

      Evidence at head (diff against merge base 816a0aab):
      -…
      Out of Scope Changes checkPassedThe head changes are three items: a one-line source change in queriesObserver.ts (passes observer-match query hashes into #combineResult from #notify), a regression test in queriesObserver.test.tsx, a…
      Full details: Title check

      Explanation

      The title names the fix area (query-core) and the core change: keeping combine hashes aligned with notification results. It describes the main change in the changeset. The emoji noise (🤖🤖🤖) is extra text that does not add meaning, but it does not change the verdict.

      Full details: Description check

      Explanation

      The description follows the repository template. It has the Changes section with motivation, a linked issue (#12626), the checklist, and the Release Impact section with the changeset box checked. It explains the root cause, the fix, and the regression test. Two checklist items are unchecked: the local test:pr run was not completed, and the size-limit comparison is pending. The author explains both gaps, so the description is mostly complete.

      Full details: Linked Issues check

      Explanation

      Objective [#12626]: a combined result must reflect the query list of the current render, even when an old query notifies before setQueries runs.

      Evidence at head (diff against merge base 816a0aab):

      • In packages/query-core/src/queriesObserver.ts, #notify now passes this.#observerMatches.map(match => match.defaultedQueryOptions.queryHash) as the third argument to #combineResult. Before this change, #notify omitted the hashes, so #lastQueryHashes kept the new list from the last optimistic render while the combined result came from the old list.
      • With the hashes passed, #notify stores the hashes of the list it actually combined. The next getOptimisticResult call then compares its hashes with a different #lastQueryHashes. queryHashesChanged becomes true, and #combineResult recomputes from the current results.
      • This matches the fix described in the issue body.

      The regression test (queriesObserver.test.tsx, +62 lines, per the change summary) covers the three-to-five-query sequence. The PR reports it fails on main and passes with the change. I did not run the test suite. The result rests on the source diff and the reported test results.

      The React and Preact adapters use the shared QueriesObserver path, so no adapter change is needed for this objective.

      Full details: Out of Scope Changes check

      Explanation

      The head changes are three items: a one-line source change in queriesObserver.ts (passes observer-match query hashes into #combineResult from #notify), a regression test in queriesObserver.test.tsx, and a patch changeset for @tanstack/query-core. The source change and the test both address the objective in #12626. The changeset documents the published fix. No unrelated behavior change appears in the reviewed diff for queriesObserver.ts.

      I did not read the full test file diff. The test scope is taken from the change summary.

      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 9026010c8a097062ac14518e17ea4b5a4b2e19bb.

      Files selected for processing: 3

      • .changeset/calm-pandas-reflect.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.

    3. tanstack-source added labelsbug

    4. TkDodo closed this

Sign in to join this conversation.