fix(query-core): keep combine hashes with notification results 🤖🤖🤖 #12632
main fromrefs/pull/12632/head·opened 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:typesacross the six configured TypeScript versions@tanstack/query-core:buildandtest:build(publint and attw)oxfmt --checkandgit 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
No actionable comments were generated in the recent review.
Walkthrough
-
When a query updates before
setQueriescommits 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#notifypasses the ordered hashes from#observerMatchesto#combineResult. The regression test checks notification data and optimistic result length when a query updates beforesetQueriescommits 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 to90260-
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:
4-
Check name Status Explanation Title check 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 t… Description check 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 e…Linked Issues check 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):
-…Out of Scope Changes check The head changes are three items: a one-line source change in queriesObserver.ts (passes observer-match query hashes into #combineResultfrom#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,
#notifynow passes this.#observerMatches.map(match => match.defaultedQueryOptions.queryHash) as the third argument to#combineResult. Before this change,#notifyomitted the hashes, so#lastQueryHasheskept the new list from the last optimistic render while the combined result came from the old list. - With the hashes passed,
#notifystores the hashes of the list it actually combined. The next getOptimisticResult call then compares its hashes with a different#lastQueryHashes. queryHashesChanged becomes true, and#combineResultrecomputes 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.
- In packages/query-core/src/queriesObserver.ts,
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
#combineResultfrom#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.
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:
fa053703-34a9-4c7f-905b-ece16f6bcd04
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.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.
-
-
sakaintp(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: 9026010c8a097062ac14518e17ea4b5a4b2e19bb
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