fix(query-core): ensure combine re-executes after cache restoration with memoized combine #9592
main fromrefs/pull/9592/head·merged by TkDodo View on GitHubFixes: #9586
Fix combine function not re-executing after cache restoration with memoized combine#
When using useQueries with a memoized combine function (via useCallback) alongside PersistQueryClientProvider, the UI fails to update after page refresh even though cached data exists.
The Problem#
We discovered this issue while working with React Query's persist functionality. After refreshing the page, the combine function would return stale pending state despite the cache containing valid data. The root cause was in QueriesObserver's setQueries method.
During our debugging process, we traced through the execution flow and found that when isRestoring changes from true to false, setQueries gets called but hits an early return condition. Since the same observer instances are reused and no index changes occur, the method returns early without updating the results or notifying listeners.
The issue becomes more critical with React 19's automatic memoization, where all functions will be memoized by default.
The Solution#
We modified the early return logic in setQueries to check if the actual results have changed. When observers haven't changed but the data or pending state differs, we now update this.#result and call this.#notify() to trigger the combine function re-execution.
if (prevObservers.length === newObservers.length && !hasIndexChange) {
const resultChanged = newResult.some((result, index) => {
const prev = this.#result[index]
return !prev || result.data !== prev.data || result.isPending !== prev.isPending
})
if (resultChanged) {
this.#result = newResult
this.#notify()
}
return
}
This ensures the UI updates correctly while maintaining the performance optimization of not recreating observers unnecessarily.
Testing#
Added test case to verify combine function works correctly with memoization and persist. All existing tests pass without modification.
Summary by CodeRabbit#
Bug Fixes
- Ensure UI updates when individual query results change (per-item shallow comparisons), even if observer identity or count remains the same.
Tests
- Added integration test validating persisted queries work with a memoized combine and render pending/data after hydration.
- Added test ensuring observers notify when underlying results change during reconfiguration.
- Updated test expectations to account for additional renders with a stable combine reference.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query-persist-client
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
View your CI Pipeline Execution ↗ for commit 44bfedbe625e7f98c6da46627e776ec78edfb082
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 33s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 27s View ↗
☁️ Nx Cloud last updated this comment at
2025-09-02 11:24:24UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-devtools-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@9592@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9592@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9592@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9592@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9592@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9592@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9592@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9592@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9592@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9592@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9592@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9592@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9592@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9592@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9592@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9592@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9592@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9592@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9592@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9592@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9592commit:
44bfedbCodecov Report#
:x: Patch coverage is
87.50000%with2 linesin your changes missing coverage. Please review. :white_check_mark: Project coverage is 59.35%. Comparing base (5221029) to head (44bfedb). :warning: Report is 1 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9592 +/- ## =========================================== + Coverage 45.23% 59.35% +14.12% =========================================== Files 208 137 -71 Lines 8335 5578 -2757 Branches 1882 1510 -372 =========================================== - Hits 3770 3311 -459 + Misses 4118 1963 -2155 + Partials 447 304 -143- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Actionable comments posted: 1
🧹 Nitpick comments (3)
packages/query-core/src/queriesObserver.ts (1)
125-140: Alternative: always update + rely on #notify gating to avoid extra emissions.Given #notify only dispatches when the combined result identity changes (replaceEqualDeep + previousResult !== newResult), we can simplify by always updating #result and calling #notify in this stable-observers branch. This keeps correctness simple and lets existing memoization handle spurious updates. Use this if you prefer clarity over a micro-optimization.
Apply this minimal alternative:
- const resultChanged = newResult.some((result, index) => { - const prev = this.#result[index] - return ( - !prev || - result.data !== prev.data || - result.isPending !== prev.isPending - ) - }) - - if (resultChanged) { - this.#result = newResult - this.#notify() - } - - return + this.#result = newResult + this.#notify() + returnpackages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (2)
94-118: Add a companion test where combine depends on fields other than data/isPending (e.g., error or isFetching).This guards against regressions if result-change detection misses non-data/isPending updates.
Proposed additional test (can live in the same file):
it('should re-run memoized combine when only error/fetching changes after persist restore', async () => { const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: 30_000, gcTime: 24 * 60 * 60 * 1000 } }, }) // Seed one successful and one errored query const persistedData: PersistedClient = { timestamp: Date.now(), buster: '', clientState: { mutations: [], queries: [ { queryHash: '["ok",1]', queryKey: ['ok', 1], state: { data: 1, dataUpdateCount: 1, dataUpdatedAt: Date.now() - 500, error: null, errorUpdateCount: 0, errorUpdatedAt: 0, fetchFailureCount: 0, fetchFailureReason: null, fetchMeta: null, isInvalidated: false, status: 'success' as const, fetchStatus: 'idle' as const, }, }, { queryHash: '["err",2]', queryKey: ['err', 2], state: { data: undefined, dataUpdateCount: 0, dataUpdatedAt: 0, error: new Error('boom'), errorUpdateCount: 1, errorUpdatedAt: Date.now() - 500, fetchFailureCount: 1, fetchFailureReason: 'boom', fetchMeta: null, isInvalidated: false, status: 'error' as const, fetchStatus: 'idle' as const, }, }, ], }, } localStorage.setItem('REACT_QUERY_OFFLINE_CACHE', JSON.stringify(persistedData)) const persister: Persister = { persistClient: (c) => { localStorage.setItem('REACT_QUERY_OFFLINE_CACHE', JSON.stringify(c)) return Promise.resolve() }, restoreClient: async () => JSON.parse(localStorage.getItem('REACT_QUERY_OFFLINE_CACHE')!) as PersistedClient, removeClient: async () => { localStorage.removeItem('REACT_QUERY_OFFLINE_CACHE') }, } function TestComponent() { const result = useQueries({ queries: [ { queryKey: ['ok', 1], queryFn: () => Promise.resolve(1), staleTime: 30_000 }, { queryKey: ['err', 2], queryFn: () => Promise.reject(new Error('boom')), staleTime: 30_000 }, ], combine: React.useCallback( (results: Array<QueryObserverResult<number, Error>>) => ({ anyError: results.some((r) => r.isError), anyFetching: results.some((r) => r.isFetching), }), [], ), }) return ( <div> <div data-testid="anyError">{String(result.anyError)}</div> <div data-testid="anyFetching">{String(result.anyFetching)}</div> </div> ) } const { getByTestId } = render( <PersistQueryClientProvider client={queryClient} persistOptions={{ persister }}> <TestComponent /> </PersistQueryClientProvider>, ) await waitFor(() => { expect(getByTestId('anyError').textContent).toBe('true') // restored cache should not be fetching expect(getByTestId('anyFetching').textContent).toBe('false') }) })
37-45: Tiny cleanup: unmount and clear QueryClient between tests to avoid cross-test leakage.Not required for this PR, but it improves test hygiene if more tests are added.
Example:
- const { getByTestId } = render( + const { getByTestId, unmount } = render( <PersistQueryClientProvider client={queryClient} persistOptions={{ persister }} > <TestComponent /> </PersistQueryClientProvider>, ) await waitFor(() => { expect(getByTestId('pending').textContent).toBe('false') expect(getByTestId('data').textContent).toBe('1,2,3') }) + + unmount() + queryClient.clear()Also applies to: 120-127, 129-133
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between a1b1279be3befc37a9d4fbb2fa79826bfb0955a4 and 79536396151869ac9a8482fb4c92c4c4a12589ec.
📒 Files selected for processing (2)
packages/query-core/src/queriesObserver.ts(1 hunks)packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (2)
packages/query-persist-client-core/src/persist.ts (2)
Persister(12-16)PersistedClient(18-22)
packages/query-core/src/types.ts (1)
QueryObserverResult(899-904)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
🔇 Additional comments (2)
packages/query-core/src/queriesObserver.ts (1)
105-121: Control flow looks sound; observer bookkeeping is maintained.- #observerMatches is refreshed before potential early return.
- Skipping updates to #observers in the stable-identity branch is correct since identities didn’t change.
- Subscriptions are stable; no unnecessary destroy/subscribe churn.
If you keep the current property-based check, please add a test where only error or fetchStatus changes after restore (data unchanged) to ensure combine re-runs. I can draft it if helpful.
Also applies to: 143-161
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (1)
37-133: Great regression test covering the persist + memoized combine scenario.The test reliably reproduces the issue and validates pending/data after hydration. Nice use of an in-memory persister with a small restore delay.
@@ -123,4 +123,18 @@123123 )124124125125 if (prevObservers.length === newObservers.length && !hasIndexChange) {126+ const resultChanged = newResult.some((result, index) => {127+ const prev = this.#result[index]128+ return (129+ !prev ||130+ result.data !== prev.data ||131+ result.isPending !== prev.isPending132+ )133+ })134+135+ if (resultChanged) {136+ this.#result = newResult137+ this.#notify()138+ }139+126140 return🛠️ Refactor suggestion
Broaden change detection: compare full result references, not only data/isPending.
Limiting detection to data and isPending risks missing updates when other fields change (e.g., status, error, fetchStatus, isFetching, isPaused, isStale). If a consumer’s combine reads any of those, UI may remain stale. A reference inequality check covers all observable result changes with minimal cost because QueryObserver emits a new result object when any tracked prop changes.
Apply this diff to generalize resultChanged:
- const resultChanged = newResult.some((result, index) => { - const prev = this.#result[index] - return ( - !prev || - result.data !== prev.data || - result.isPending !== prev.isPending - ) - }) - - if (resultChanged) { - this.#result = newResult - this.#notify() - } + const resultChanged = newResult.some( + (result, index) => result !== this.#result[index], + ) + + if (!resultChanged) return + this.#result = newResult + this.#notify()📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (prevObservers.length === newObservers.length && !hasIndexChange) { const resultChanged = newResult.some( (result, index) => result !== this.#result[index], ) if (!resultChanged) return this.#result = newResult this.#notify() return }🤖 Prompt for AI Agents
In packages/query-core/src/queriesObserver.ts around lines 125 to 140, the change detection only compares result.data and result.isPending which misses other observable fields; replace that predicate with a full reference inequality check (e.g., compare newResult[index] !== this.#result[index]) so any updated result object triggers update; if any reference differs, assign this.#result = newResult and call this.#notify() before returning.✅ Addressed in commits ad0ca9f to 32a55ad
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/query-core/src/__tests__/queriesObserver.test.tsx (1)
377-379: Timers may be unnecessary; prefer awaiting microtasks or keep if needed for consistencynotifyManager in query-core often schedules microtasks rather than timers. vi.advanceTimersByTimeAsync(0) is harmless but might be redundant here. If the test remains flaky in CI, keep it; otherwise consider awaiting a microtask flush for determinism:
await Promise.resolve()📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 79536396151869ac9a8482fb4c92c4c4a12589ec and f860b8d4455e63d4a5038bbf1721c1f020f5000c.
📒 Files selected for processing (2)
packages/query-core/src/__tests__/queriesObserver.test.tsx(1 hunks)packages/query-core/src/queriesObserver.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/query-core/src/queriesObserver.ts
🧰 Additional context used
🧬 Code graph analysis (1)
packages/query-core/src/__tests__/queriesObserver.test.tsx (2)
packages/query-core/src/queriesObserver.ts (3)
observer(269-275)QueriesObserver(35-292)result(201-216)
packages/query-core/src/types.ts (1)
QueryObserverResult(899-904)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
@@ -349,0 +370,21 @@370+ })371+372+ observer.setQueries([373+ { queryKey: key1, queryFn: queryFn1, staleTime: Infinity },374+ { queryKey: key2, queryFn: queryFn2, staleTime: Infinity },375+ ])376+377+ await vi.advanceTimersByTimeAsync(0)378+379+ unsubscribe()380+381+ expect(results.length).toBeGreaterThanOrEqual(2)382+ expect(results[0]).toMatchObject([383+ { status: 'success', data: 1 },384+ { status: 'success', data: 2 },385+ ])386+ expect(results[results.length - 1]).toMatchObject([387+ { status: 'success', data: 1 },388+ { status: 'success', data: 2 },389+ ])390+ })🛠️ Refactor suggestion
Test does not actually prove early-return notifications; change doesn’t affect data/isPending/error
- Setting staleTime: Infinity does not change result.data, result.isPending, or result.error, which are the fields used by the new early-return detection. As written, this test can pass even if no notification happens during setQueries, because subscribing typically emits at least once, making results.length >= 2 trivially true.
- The assertions only check that initial and final results are both success with the same data (1 and 2), so they don’t demonstrate that an extra notify occurred due to setQueries.
Strengthen the test by causing a real result change without changing observer identities (e.g., change select to map the existing data), then assert an additional emission occurred and verify the transformed data. Suggested patch:
test('should notify when results change during early return', async () => { const key1 = queryKey() const key2 = queryKey() const queryFn1 = vi.fn().mockReturnValue(1) const queryFn2 = vi.fn().mockReturnValue(2) queryClient.setQueryData(key1, 1) queryClient.setQueryData(key2, 2) const observer = new QueriesObserver(queryClient, [ { queryKey: key1, queryFn: queryFn1 }, { queryKey: key2, queryFn: queryFn2 }, ]) - const results: Array<Array<QueryObserverResult>> = [] - results.push(observer.getCurrentResult()) - - const unsubscribe = observer.subscribe((result) => { - results.push(result) - }) + const results: Array<Array<QueryObserverResult>> = [] + results.push(observer.getCurrentResult()) // snapshot without listeners + const onUpdate = vi.fn((result: Array<QueryObserverResult>) => { + results.push(result) + }) + const unsubscribe = observer.subscribe(onUpdate) + const baseline = results.length - observer.setQueries([ - { queryKey: key1, queryFn: queryFn1, staleTime: Infinity }, - { queryKey: key2, queryFn: queryFn2, staleTime: Infinity }, - ]) + // Change options only (same observers), but map data via `select` + // so result.data actually changes and should trigger notify in the "early return" path. + observer.setQueries([ + { + queryKey: key1, + queryFn: queryFn1, + select: (d: number) => d + 100, + }, + { + queryKey: key2, + queryFn: queryFn2, + select: (d: number) => d + 100, + }, + ]) await vi.advanceTimersByTimeAsync(0) unsubscribe() - expect(results.length).toBeGreaterThanOrEqual(2) - expect(results[0]).toMatchObject([ - { status: 'success', data: 1 }, - { status: 'success', data: 2 }, - ]) - expect(results[results.length - 1]).toMatchObject([ - { status: 'success', data: 1 }, - { status: 'success', data: 2 }, - ]) + // At least one additional emission should occur due to setQueries + expect(results.length).toBeGreaterThan(baseline) + // And the final emission should reflect the transformed data + expect(results[results.length - 1]).toMatchObject([ + { status: 'success', data: 101 }, + { status: 'success', data: 102 }, + ]) })📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
test('should notify when results change during early return', async () => { const key1 = queryKey() const key2 = queryKey() const queryFn1 = vi.fn().mockReturnValue(1) const queryFn2 = vi.fn().mockReturnValue(2) queryClient.setQueryData(key1, 1) queryClient.setQueryData(key2, 2) const observer = new QueriesObserver(queryClient, [ { queryKey: key1, queryFn: queryFn1 }, { queryKey: key2, queryFn: queryFn2 }, ]) const results: Array<Array<QueryObserverResult>> = [] results.push(observer.getCurrentResult()) // snapshot without listeners const onUpdate = vi.fn((result: Array<QueryObserverResult>) => { results.push(result) }) const unsubscribe = observer.subscribe(onUpdate) const baseline = results.length // Change options only (same observers), but map data via `select` // so result.data actually changes and should trigger notify in the "early return" path. observer.setQueries([ { queryKey: key1, queryFn: queryFn1, select: (d: number) => d + 100, }, { queryKey: key2, queryFn: queryFn2, select: (d: number) => d + 100, }, ]) await vi.advanceTimersByTimeAsync(0) unsubscribe() // At least one additional emission should occur due to setQueries expect(results.length).toBeGreaterThan(baseline) // And the final emission should reflect the transformed data expect(results[results.length - 1]).toMatchObject([ { status: 'success', data: 101 }, { status: 'success', data: 102 }, ]) })🤖 Prompt for AI Agents
In packages/query-core/src/__tests__/queriesObserver.test.tsx around lines 351 to 390, the test sets staleTime: Infinity but does not change result.data, result.isPending, or result.error (the fields used to detect early-return notifications), so the test can pass without proving that setQueries caused an extra notify; modify the test to produce a real change to one of those fields without replacing observer identities — for example, add a select that maps existing data to a new value (or update the cached data/queryFn to return a different value) so that result.data changes when setQueries runs, keep the same queryKey/queryFn identities, assert that results length increased by at least one beyond the initial subscription emission, and verify the emitted transformed data to prove the extra notification occurred.✅ Addressed in commit 5227bc3
TkDodo reviewed
packages/query-core/src/queriesObserver.ts lines 130 to 132Outdated@@ -123,3 +123,10 @@123123 )124124125125 if (prevObservers.length === newObservers.length && !hasIndexChange) {126+ const resultChanged = newResult.some((result, index) => {127+ const prev = this.#result[index]128+ return (129+ !prev ||130+ result.data !== prev.data ||131+ result.isPending !== prev.isPending ||132+ result.error !== prev.errorchecking for these 3 props seems a bit random. I think we’d want to use our
shallowEqualObjectshelper from the query-core to just compareresultwithprev. This is also what we do insidequeryObserver:I found that using shallowEqualObjects causes test failures because it compares ALL properties of QueryObserverResult, including timestamps like dataUpdatedAt. This means it detects changes even when the actual data hasn't changed. The individual property comparison (data, isPending, error) passes all tests. Should we stick with the explicit property checks, or would you prefer a different approach?
but if
dataUpdatedAtchanges, we need to inform observers about it, right? A component could renderdataUpdatedAton the screen so it will want that information to be correct 🤔You're absolutely right. Components could render any property including dataUpdatedAt, so we need to detect all changes. I'll use shallowEqualObjects as you suggested. The test failures appear to be because the tests expect combine not to re-run when metadata changes, but that expectation seems incorrect. When any property changes (even metadata), combine should re-execute to ensure the UI stays in sync. Should I update the test expectations to reflect this correct behavior?
test file: packages/react-query/src/tests/useQueries.test.tsx >
should optimize combine if it is a stable referenceI've updated the test expectations to account for the shallowEqualObjects comparison. The tests now expect combine to re-run when metadata changes occur. However, I'm not entirely confident whether having combine re-run on metadata changes is the intended behavior from this PR... Perhaps there's a performance consideration I'm missing, or my implementation itself might be incorrect. The tests are passing with the updated expectations, but I believe a different approach might be more appropriate. I defer to your judgment on whether this is the desired behavior.
yeah that test seems slightly off, and actually shows the bug: we get new metadata (
dataUpdatedAt), which is part ofresultspassed tocombine, but we don’t run it :/I think I was basing this test off of
selectonuseQuery, but the difference is thatselectonly getsdatapassed whilecombinegets passed everything.Should I update the test expectations to reflect this correct behavior?
yes please, and we need to test combine with a stable reference differently, likely by triggering a re-render differently, e.g. just having an
incrementthat triggers an unrelated re-render, because in those cases, a stable combine shouldn’t re-run.TkDodo reviewed
packages/query-core/src/queriesObserver.ts lines 136 to 139Outdated@@ -123,3 +123,17 @@123123 )124124125125 if (prevObservers.length === newObservers.length && !hasIndexChange) {126+ const resultChanged = newResult.some((result, index) => {127+ const prev = this.#result[index]128+ return (129+ !prev ||130+ result.data !== prev.data ||131+ result.isPending !== prev.isPending ||132+ result.error !== prev.error133+ )134+ })135+136+ if (resultChanged) {137+ this.#result = newResult138+ this.#notify()139+ }I don’t really like the early returns here and the duplicated assignment to
#resultas well as calls to#notify, makes it kinda hard to read what is going on. I think we’d want something like this:- first, check for
hasIndexChange- if we have an index change, we want to compute the difference for observers and destroy / re-create them, then assign
this.#observers = newObservers
- if we have an index change, we want to compute the difference for observers and destroy / re-create them, then assign
- then, we check for
hasResultChange- if that’s the case, we want to assign
this.#result = newResult, then check for listeners, then callthis.#notify().
- if that’s the case, we want to assign
this flow ties logic better to the conditions: when indices change, we need to re-do observers and assign them; when result changes, we need to re-assign it. If anything changes, we need to
notify(I’m assuming here that if indices change, we’ll also get a resultChange)Thank you for the detailed feedback! I've updated the code based on your suggestions. To be honest, I was struggling to find the right approach, but the CodeRabbitAI feedback really helped clarify the path forward.
Would appreciate another review when you have time. Thanks!
- first, check for
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/query-core/src/queriesObserver.ts (2)
125-157: Streamline control flow: compute both flags, assign once, notify once.Reduce early-returns and duplicated state updates to improve readability and make change handling explicit.
Apply this diff:
- const hasIndexChange = newObservers.some( - (observer, index) => observer !== prevObservers[index], - ) - - if (prevObservers.length === newObservers.length && !hasIndexChange) { - const hasResultChangeOnly = newResult.some((result, index) => { - const prev = this.#result[index] - return !prev || !shallowEqualObjects(result, prev) - }) - - if (hasResultChangeOnly) { - this.#result = newResult - this.#notify() - } - - return - } - - this.#observers = newObservers - this.#result = newResult - - if (!this.hasListeners()) { - return - } - - difference(prevObservers, newObservers).forEach((observer) => { - observer.destroy() - }) - - difference(newObservers, prevObservers).forEach((observer) => { - observer.subscribe((result) => { - this.#onUpdate(observer, result) - }) - }) - - this.#notify() + const hasIndexChange = newObservers.some( + (observer, index) => observer !== prevObservers[index], + ) + const hasResultChange = + prevObservers.length === newObservers.length && !hasIndexChange + ? newResult.some((result, index) => { + const prev = this.#result[index] + return !prev || !shallowEqualObjects(result, prev) + }) + : true + + if (!hasIndexChange && !hasResultChange) return + + if (hasIndexChange) { + this.#observers = newObservers + } + this.#result = newResult + + if (!this.hasListeners()) return + + if (hasIndexChange) { + difference(prevObservers, newObservers).forEach((observer) => { + observer.destroy() + }) + difference(newObservers, prevObservers).forEach((observer) => { + observer.subscribe((result) => { + this.#onUpdate(observer, result) + }) + }) + } + + this.#notify()
126-133: Naming nit: drop “Only” from hasResultChangeOnly.Inside the no-index-change branch it’s implicit; “hasResultChange” reads cleaner.
-const hasResultChangeOnly = newResult.some(… +const hasResultChange = newResult.some(… … -if (hasResultChangeOnly) { +if (hasResultChange) {📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 5227bc3951dd55055fc011e3ce4324cd88fcb8a5 and 32a55ad556c8cf47d5f825813aa92ad320860380.
📒 Files selected for processing (1)
packages/query-core/src/queriesObserver.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/query-core/src/queriesObserver.ts (1)
packages/query-core/src/utils.ts (1)
shallowEqualObjects(297-312)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
🔇 Additional comments (2)
packages/query-core/src/queriesObserver.ts (2)
4-4: Good: align equality semantics with core helper.Importing and using shallowEqualObjects mirrors queryObserver’s comparison strategy and avoids missing changes on non-data fields.
125-136: Bug fix validated: stable-observers path now notifies on real result changes.The shallow per-index compare against this.#result ensures combine re-runs after persist restore even when observers/indices are stable.
Actionable comments posted: 0
♻️ Duplicate comments (1)
packages/query-core/src/queriesObserver.ts (1)
125-137: Remove debug logging and keep comparisons deterministic.console.log in core hot paths will leak noise in production and can be a perf drag.
Apply this minimal diff to drop logs:
- ? newResult.some((result, index) => { - const prev = this.#result[index] - const isChanged = !prev || !shallowEqualObjects(result, prev) - console.log(`Result ${index}:`, { - hasData: !!result.data, - prevData: prev?.data, - dataChanged: result.data !== prev?.data, - }) - return isChanged - }) + ? newResult.some((result, index) => { + const prev = this.#result[index] + return !prev || !shallowEqualObjects(result, prev) + })Optional: compare by reference for cheaper, broader change detection (QueryObserver emits new objects on any prop change).
- ? newResult.some((result, index) => { - const prev = this.#result[index] - return !prev || !shallowEqualObjects(result, prev) - }) + ? newResult.some((result, index) => result !== this.#result[index])If you take the optional path, also drop the unused import:
-import { replaceEqualDeep, shallowEqualObjects } from './utils' +import { replaceEqualDeep } from './utils'🧹 Nitpick comments (1)
packages/query-core/src/queriesObserver.ts (1)
139-159: Simplify control flow: separate index vs. result changes; avoid redundant assignments.This mirrors the intent from earlier feedback and improves readability.
Apply:
- if (!hasIndexChange && !hasResultChange) return - - if (hasIndexChange) { - this.#observers = newObservers - } - this.#result = newResult - - if (!this.hasListeners()) return - - if (hasIndexChange) { - difference(prevObservers, newObservers).forEach((observer) => { - observer.destroy() - }) - difference(newObservers, prevObservers).forEach((observer) => { - observer.subscribe((result) => { - this.#onUpdate(observer, result) - }) - }) - } - - this.#notify() + const anythingChanged = hasIndexChange || hasResultChange + if (!anythingChanged) return + + if (hasIndexChange) { + this.#observers = newObservers + } + if (hasResultChange) { + this.#result = newResult + } + + if (!this.hasListeners()) return + + if (hasIndexChange) { + difference(prevObservers, newObservers).forEach((observer) => { + observer.destroy() + }) + difference(newObservers, prevObservers).forEach((observer) => { + observer.subscribe((result) => { + this.#onUpdate(observer, result) + }) + }) + } + + this.#notify()📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 32a55ad556c8cf47d5f825813aa92ad320860380 and 999c8d2da2d28dcaddd1745a06b97585f6d22d7e.
📒 Files selected for processing (1)
packages/query-core/src/queriesObserver.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/query-core/src/queriesObserver.ts (1)
packages/query-core/src/utils.ts (1)
shallowEqualObjects(305-320)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
🔇 Additional comments (1)
packages/query-core/src/queriesObserver.ts (1)
4-4: Import choice aligns with queryObserver usage.Pulling in shallowEqualObjects is consistent with queryObserver’s comparison strategy. Good.
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
Actionable comments posted: 0
🧹 Nitpick comments (5)
packages/react-query/src/__tests__/useQueries.test.tsx (3)
1213-1213: Reduce test flakiness by avoiding hard-coded acceptable counts.Accepting [4,5,6] is brittle across timing/impl changes. Prefer explicit bounds.
- expect([4, 5, 6]).toContain(results.length) + expect(results.length).toBeGreaterThanOrEqual(4) + expect(results.length).toBeLessThanOrEqual(6)
1382-1384: Fix misleading inline comment to match the new expectation.Comment says “no increase” but we now expect one extra call.
- // no increase because just a re-render - expect(spy).toHaveBeenCalledTimes(4) + // one extra call due to recomputing the combined result on rerender + expect(spy).toHaveBeenCalledTimes(4)
1394-1396: Adjust comment to reflect why the count increases to 7.Clarify that the refetch sequence drives three combine calls (pending → partial → settled).
- // two value changes = two re-renders + // refetch with new values triggers: both pending -> one pending -> both resolved expect(spy).toHaveBeenCalledTimes(7)packages/query-core/src/queriesObserver.ts (2)
125-132: Micro-optimization: short-circuit on reference equality before shallow compare.Most QueryObserver results are stable by reference when unchanged. Add a ref check to avoid iterating keys.
- ? newResult.some((result, index) => { - const prev = this.#result[index] - return !prev || !shallowEqualObjects(result, prev) - }) + ? newResult.some((result, index) => { + const prev = this.#result[index] + return !prev || (result !== prev && !shallowEqualObjects(result, prev)) + })
141-152: Avoid retaining obsolete observers when there are no listeners.If indices change with no listeners, we return before destroying removed observers. It’s minor, but proactively cleaning them up prevents transient retention during lifecycles where setQueries is called pre-subscription.
- if (!this.hasListeners()) return - - if (hasIndexChange) { - difference(prevObservers, newObservers).forEach((observer) => { - observer.destroy() - }) - difference(newObservers, prevObservers).forEach((observer) => { - observer.subscribe((result) => { - this.#onUpdate(observer, result) - }) - }) - } + if (hasIndexChange) { + // Always clean up obsolete observers to avoid leaks even without listeners + difference(prevObservers, newObservers).forEach((observer) => { + observer.destroy() + }) + if (this.hasListeners()) { + difference(newObservers, prevObservers).forEach((observer) => { + observer.subscribe((result) => { + this.#onUpdate(observer, result) + }) + }) + } + } + + if (!this.hasListeners()) return📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 999c8d2da2d28dcaddd1745a06b97585f6d22d7e and 4bc297b538476c69ad2a48809193e4bf5be3c703.
📒 Files selected for processing (2)
packages/query-core/src/queriesObserver.ts(2 hunks)packages/react-query/src/__tests__/useQueries.test.tsx(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/query-core/src/queriesObserver.ts (1)
packages/query-core/src/utils.ts (1)
shallowEqualObjects(305-320)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (2)
packages/query-core/src/queriesObserver.ts (2)
4-4: Aligns with queryObserver: using shallowEqualObjects for result comparisons.Good call to match core semantics and catch metadata changes (e.g., dataUpdatedAt, fetchStatus).
133-141: Clearer control flow.Early-return + separating index/result changes improves readability and mirrors intended semantics.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/react-query/src/__tests__/useQueries.test.tsx (3)
1213-1213: Relax render-count assertion to reduce flakiness.Render counts can vary slightly across environments/schedulers. Prefer a bounded range over enumerating discrete values.
- expect([4, 5, 6]).toContain(results.length) + expect(results.length).toBeGreaterThanOrEqual(4) + expect(results.length).toBeLessThanOrEqual(6)
1382-1384: Confirm intent: extra combine call on rerender with stable callback.This weakens the “optimize combine for stable reference” story by recomputing on parent rerender even when inputs don’t change. If intentional, consider asserting the last payload to clarify that it’s a recomputation without value change.
// one extra call due to recomputing the combined result on rerender expect(spy).toHaveBeenCalledTimes(4) + expect(spy).toHaveBeenLastCalledWith({ + combined: true, + res: 'first result:0,second result:0', + })
1394-1396: Avoid brittle absolute spy call counts; assert relative +3 after refetch.Make the test resilient to incidental extra emissions while still validating the sequence (pending → partial → resolved).
- // refetch with new values triggers: both pending -> one pending -> both resolved - expect(spy).toHaveBeenCalledTimes(7) + // refetch with new values triggers: both pending -> one pending -> both resolved (+3) + expect(spy).toHaveBeenCalledTimes(baseCalls + 3)Add this just before
client.refetchQueries()to capture the baseline:const baseCalls = spy.mock.calls.length📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 4bc297b538476c69ad2a48809193e4bf5be3c703 and 6a20d93b73880a24bdb9df613abc70ea78f82b80.
📒 Files selected for processing (1)
packages/react-query/src/__tests__/useQueries.test.tsx(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
TkDodo reviewed
please add another test that verifies that
combineisn’t called when an unrelated re-render happensplease add another test that verifies that
combineisn’t called when an unrelated re-render happens44bfedb
I have added the test. Comments have been appropriately added by referring to existing other tests.
Actionable comments posted: 0
♻️ Duplicate comments (1)
packages/react-query/src/__tests__/useQueries.test.tsx (1)
1662-1743: Great addition: guards against unnecessary combine executions on unrelated re-rendersThis directly addresses the earlier reviewer request to add such a test and protects the optimization going forward.
🧹 Nitpick comments (1)
packages/react-query/src/__tests__/useQueries.test.tsx (1)
1213-1213: Prefer explicit range assertions over discrete setUsing a range communicates intent and is less brittle than enumerating acceptable lengths.
- expect([4, 5, 6]).toContain(results.length) + expect(results.length).toBeGreaterThanOrEqual(4) + expect(results.length).toBeLessThanOrEqual(6)📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between 6a20d93b73880a24bdb9df613abc70ea78f82b80 and 44bfedbe625e7f98c6da46627e776ec78edfb082.
📒 Files selected for processing (1)
packages/react-query/src/__tests__/useQueries.test.tsx(4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/react-query/src/__tests__/useQueries.test.tsx (4)
packages/query-core/src/queriesObserver.ts (2)
queries(234-260)result(194-209)
packages/react-query/src/useQueries.ts (1)
useQueries(208-332)
packages/query-core/src/utils.ts (1)
sleep(362-366)
packages/query-core/src/types.ts (1)
QueryObserverResult(899-904)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
🔇 Additional comments (2)
packages/react-query/src/__tests__/useQueries.test.tsx (2)
1382-1383: Unrelated re-render should not recompute stable combine — adjust expectationThis contradicts the new test added below (“should not re-run stable combine on unrelated re-render”), which asserts no extra combine call on an unrelated re-render with a stable reference. Align this test to the same behavior.
- // one extra call due to recomputing the combined result on rerender - expect(spy).toHaveBeenCalledTimes(4) + // stable combine should not recompute on unrelated rerender + expect(spy).toHaveBeenCalledTimes(3)
1394-1396: Call count after refetch should reflect only the refetch transitionsIf the rerender did not recompute combine, the subsequent refetch with new values should add three calls (both pending -> one pending -> both resolved), bringing the total from 3 to 6.
- // refetch with new values triggers: both pending -> one pending -> both resolved - expect(spy).toHaveBeenCalledTimes(7) + // refetch with new values triggers: both pending -> one pending -> both resolved + expect(spy).toHaveBeenCalledTimes(6)TkDodo approved these changes
TkDodo merged this564a4f6
TkDodo closed this
Sign in to join this conversation.
Walkthrough#
Adds per-index shallow-equality result-change detection to QueriesObserver so subscribers are notified when underlying per-query results change even if observer identities/count remain the same. Adds tests for early-return notifications and for useQueries with persistence + a memoized combine.
Changes#
packages/query-core/src/queriesObserver.tsshallowEqualObjects; compute per-indexhasResultChangeby shallowly comparingnewResultvsthis.#result; return early only when neither indices nor per-index results change; updatethis.#result/this.#observers; subscribe/unsubscribe observers when indices change; always callthis.#notify()after updates.packages/query-core/src/__tests__/queriesObserver.test.tsxselect) even if observer list is stable.packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsxuseQuerieswith a memoizedcombine, and assertsisPendingbecomes false and combined data is restored after hydration.packages/react-query/src/__tests__/useQueries.test.tsxcombineis not recomputed on unrelated re-renders.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
Assessment against linked issues#
✨ Finishing Touches
🧪 Generate unit tests
🪧 Tips
Chat#
There are 3 ways to chat with CodeRabbit:
I pushed a fix in commit <commit_id>, please review it.Open a follow-up GitHub issue for this discussion.@coderabbitaiin a new review comment at the desired location with your query.@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.Support#
Need help? Create a ticket on our support page for assistance with any issues or questions.
CodeRabbit Commands (Invoked using PR/Issue comments)#
Type
@coderabbitai helpto get the list of available commands.Other keywords and placeholders#
@coderabbitai ignoreor@coderabbit ignoreanywhere in the PR description to prevent this PR from being reviewed.@coderabbitai summaryto generate the high-level summary at a specific location in the PR description.@coderabbitaianywhere in the PR title to generate the title automatically.CodeRabbit Configuration File (
.coderabbit.yaml)#.coderabbit.yamlfile to the root of your repository.# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.jsonStatus, Documentation and Community#