fix(query-core/queriesObserver): keep combined result in sync when a query updates before setQueries #12638
main fromrefs/pull/12638/head·opened 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
useQueriescould 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.
- Fixed an issue where combined results from
- 1
No actionable comments were generated in the recent review.
Walkthrough
-
QueriesObserver.#notifynow passes the hashes of its current observer matches to#combineResult. A regression test covers an existing query update beforesetQueriesapplies 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#notifypasses 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
- TanStack/query#12633: Makes the same
#notifyhash-passing change to address the same stale combined result. - TanStack/query#12632: Makes the same change for the same stale-result behavior.
Suggested reviewers:
sukvvon,tkdodo - TanStack/query#12633: Makes the same
Merge Risk:
Minimal · up to109ed-
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:
4-
Check name Status Explanation Title check 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… Description check 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 sectio…Linked Issues check Issue #12626requires that the combineduseQueriesresult match the current render's query list after a query updates beforesetQueriesruns. At the reviewed head,#notifyin `packages/query-cor…Out of Scope Changes check The whole-PR changes are a one-line-group change in #notify(three added lines passing the query hashes), a regression test inpackages/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
#12626requires that the combineduseQueriesresult match the current render's query list after a query updates beforesetQueriesruns. At the reviewed head,#notifyinpackages/query-core/src/queriesObserver.tsnow passesthis.#observerMatches.map((match) => match.defaultedQueryOptions.queryHash)to#combineResult. Before this change,#notifypassed no hashes, so#lastQueryHashescould 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.#observerMatchesstill holds A (3 queries), soqueryHashesChangedis true, the result is recomputed from 3 entries, and#lastQueryHashesbecomes A. The next render with B sees a hash mismatch and recomputes from 5 entries. The hashes always correspond to the input array, since#observerMatchesand#resultare updated together insetQueries. The PR also adds a regression test inqueriesObserver.test.tsxthat 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 inpackages/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 ofqueriesObserver.ts. The PR description mentions related PRs#12632and#12633as 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.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/query
- Branch: main
- File: .coderabbit.yaml
- Commit: f6689e0
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
3b9126f0-aa8d-431c-af6c-6b3f43f6aada
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.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.
-
-
mgarcia-improving(GitHub profile, opens in a new tab) linked this to fix #12626
tanstack-source added labelsbug
TkDodo closed this
Sign in to join this conversation.
🦋 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
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