Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core/queriesObserver): keep combined result in sync when a query updates before setQueries #12638

Closedmgarcia-improving(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12638/headopened View on GitHub

🎯 Changes

QueriesObserver stores the query hashes of the list it last combined in #lastQueryHashes, and uses them to decide whether to recompute combine. #notify runs when a query updates, but it combined the current observers without passing those hashes.

If a query updated after a render with a longer query list but before setQueries ran, #notify combined the old list. The next render saw matching hashes and returned that stale result, so useQueries returned fewer results than queries.

This PR passes the hashes of the observers #notify combines, so the stored hashes always match the cached result.

Regression test: render A, call setQueries(A, { combine }), render A+B, resolve a query in A, then render A+B again and expect two results. The test fails without this change.

Fixes #12626

Related: #12632 and #12633 contain the same source change. I'm happy to close this one if a maintainer prefers either of those.

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

Test note: pnpm run test:pr fails on unrelated example and integration apps (Next.js build stops on a security-advisory check, the Nuxt example's types are not generated locally, and the SvelteKit example's eslint config fails). Locally, @tanstack/query-core and @tanstack/react-query pass test:lib (1641 and 620 tests), test:types, test:oxlint, test:build, build, test:sherif, test:knip, and test:docs.

🚀 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 results from useQueries could become out of sync if an existing query updated while another query was being added. Combined results now remain consistent as the query list changes and fetching completes.
    1. 🦋 Changeset detected

      Latest commit: 109ed17447e6185b4ae02387c712a9e214526e09

      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

      QueriesObserver.#notify now passes the hashes of its current observer matches to #combineResult. A regression test covers an existing query update before setQueries applies an expanded list. A patch changeset records the query-core change.

      Changes: Combined query result consistency

      Layer / File(s)Summary
      Notification hash tracking and regression coverage
      packages/query-core/src/queriesObserver.ts, packages/query-core/src/__tests__/queriesObserver.test.tsx, .changeset/stale-use-queries-combine.md
      #notify passes current matched query hashes to #combineResult. The regression test verifies the expanded combined result still has two items after the existing queries resolve. 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

      Suggested reviewers: sukvvon, tkdodo

      Merge Risk: Minimal · up to 109ed

      This fixes useQueries returning a combined result shorter than the current query list when an existing query updates before setQueries runs. The change is small and covered by a regression test, so merge risk is minimal.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the actual fix: QueriesObserver now keeps the combined result in sync when a query updates before setQueries runs. It matches the source change in queriesObserver.ts and the regression…
      Description checkPassedThe description follows the template. It has a Changes section that explains the bug, the cause, and the fix, and it links the issue (#12626). The checklist is filled in, and the release impact sectio…
      Linked Issues checkPassedIssue #12626 requires that the combined useQueries result match the current render's query list after a query updates before setQueries runs. At the reviewed head, #notify in `packages/query-cor…
      Out of Scope Changes checkPassedThe whole-PR changes are a one-line-group change in #notify (three added lines passing the query hashes), a regression test in packages/query-core/src/__tests__/queriesObserver.test.tsx (+48 lines…
      Full details: Title check

      Explanation

      The title names the actual fix: QueriesObserver now keeps the combined result in sync when a query updates before setQueries runs. It matches the source change in queriesObserver.ts and the regression test. The title is specific and uses the conventional fix(scope) format, though it is long at 100 characters.

      Full details: Description check

      Explanation

      The description follows the template. It has a Changes section that explains the bug, the cause, and the fix, and it links the issue (#12626). The checklist is filled in, and the release impact section marks that a changeset was generated. Two checklist items are unchecked: local test:pr was not fully run, and the AI policy box is unchecked. The author does explain why test:pr fails and lists the lib, type, and build checks that passed locally. The description therefore meets the template's requirements.

      Full details: Linked Issues check

      Explanation

      Issue #12626 requires that the combined useQueries result match the current render's query list after a query updates before setQueries runs. At the reviewed head, #notify in packages/query-core/src/queriesObserver.ts now passes this.#observerMatches.map((match) => match.defaultedQueryOptions.queryHash) to #combineResult. Before this change, #notify passed no hashes, so #lastQueryHashes could keep the new list while the cached result came from the old list. Tracing the issue sequence with the new code: after the render with B (5 queries) sets #lastQueryHashes = B, an update for query A calls #notify. #observerMatches still holds A (3 queries), so queryHashesChanged is true, the result is recomputed from 3 entries, and #lastQueryHashes becomes A. The next render with B sees a hash mismatch and recomputes from 5 entries. The hashes always correspond to the input array, since #observerMatches and #result are updated together in setQueries. The PR also adds a regression test in queriesObserver.test.tsx that covers this sequence, as described in the summary. The issue has no non-coding requirements. Reasoning was checked against the diff and the file at head. I did not run the test suite.

      Full details: Out of Scope Changes check

      Explanation

      The whole-PR changes are a one-line-group change in #notify (three added lines passing the query hashes), a regression test in packages/query-core/src/__tests__/queriesObserver.test.tsx (+48 lines), and a patch changeset for @tanstack/query-core. The test and changeset directly support the fix for #12626. No unrelated source changes appear in the summary or the diff of queriesObserver.ts. The PR description mentions related PRs #12632 and #12633 as containing the same change. This PR does not include their code beyond what is in this diff.

      Finishing Touches

      Generate unit tests (beta)

      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Warning

      ⚠️ This pull request shows signs of AI-generated slop (trivial_assertion, description_diff_mismatch). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

      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 f6689e04be493e2e83f9c93d7e478f92c3e5d328 and 109ed17447e6185b4ae02387c712a9e214526e09.

      Files selected for processing: 3

      • .changeset/stale-use-queries-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.

    3. tanstack-source added labelsbug

    4. TkDodo closed this

Sign in to join this conversation.