-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(query-core): ensure combine re-executes after cache restoration with memoized combine #9592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TkDodo
merged 13 commits into
TanStack:main
from
joseph0926:fix/memoized-combine-persist-restore
Sep 2, 2025
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7953639
fix(query-core): ensure combine re-executes after cache restoration w…
joseph0926 7eaaad7
fix(query-core): add error field to result change detection
joseph0926 f860b8d
test(query-core): add test for QueriesObserver early return notification
joseph0926 5227bc3
test(query-core): improve test for QueriesObserver early return notif…
joseph0926 5540f61
Merge branch 'main' into fix/memoized-combine-persist-restore
joseph0926 e5a60aa
Merge branch 'main' into fix/memoized-combine-persist-restore
joseph0926 32a55ad
refactor(query-core): use shallowEqualObjects for result comparison
joseph0926 e351336
Merge branch 'main' into fix/memoized-combine-persist-restore
TkDodo 999c8d2
refactor(query-core): improve setQueries flow and fix persist/restore…
joseph0926 c4db285
refactor(query-core): improve setQueries flow and remove log
joseph0926 4bc297b
test(react-query): update useQueries expectations for metadata change…
joseph0926 6a20d93
test(react-query): fix test comments
joseph0926 44bfedb
test(react-query): add test for stable combine optimization on unrela…
joseph0926 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
| import { render, waitFor } from '@testing-library/react' | ||
| import * as React from 'react' | ||
| import { QueryClient, useQueries } from '@tanstack/react-query' | ||
| import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client' | ||
| import type { | ||
| PersistedClient, | ||
| Persister, | ||
| } from '@tanstack/query-persist-client-core' | ||
| import type { QueryObserverResult } from '@tanstack/react-query' | ||
|
|
||
| describe('useQueries with persist and memoized combine', () => { | ||
| const storage: { [key: string]: string } = {} | ||
|
|
||
| beforeEach(() => { | ||
| Object.defineProperty(window, 'localStorage', { | ||
| value: { | ||
| getItem: (key: string) => storage[key] || null, | ||
| setItem: (key: string, value: string) => { | ||
| storage[key] = value | ||
| }, | ||
| removeItem: (key: string) => { | ||
| delete storage[key] | ||
| }, | ||
| clear: () => { | ||
| Object.keys(storage).forEach((key) => delete storage[key]) | ||
| }, | ||
| }, | ||
| writable: true, | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| Object.keys(storage).forEach((key) => delete storage[key]) | ||
| }) | ||
|
|
||
| it('should update UI when combine is memoized with persist', async () => { | ||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| staleTime: 30_000, | ||
| gcTime: 1000 * 60 * 60 * 24, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const persister: Persister = { | ||
| persistClient: (client: PersistedClient) => { | ||
| storage['REACT_QUERY_OFFLINE_CACHE'] = JSON.stringify(client) | ||
| return Promise.resolve() | ||
| }, | ||
| restoreClient: async () => { | ||
| const stored = storage['REACT_QUERY_OFFLINE_CACHE'] | ||
| if (stored) { | ||
| await new Promise((resolve) => setTimeout(resolve, 10)) | ||
| return JSON.parse(stored) as PersistedClient | ||
| } | ||
| return undefined | ||
| }, | ||
| removeClient: () => { | ||
| delete storage['REACT_QUERY_OFFLINE_CACHE'] | ||
| return Promise.resolve() | ||
| }, | ||
| } | ||
|
|
||
| const persistedData: PersistedClient = { | ||
| timestamp: Date.now(), | ||
| buster: '', | ||
| clientState: { | ||
| mutations: [], | ||
| queries: [1, 2, 3].map((id) => ({ | ||
| queryHash: `["post",${id}]`, | ||
| queryKey: ['post', id], | ||
| state: { | ||
| data: id, | ||
| dataUpdateCount: 1, | ||
| dataUpdatedAt: Date.now() - 1000, | ||
| error: null, | ||
| errorUpdateCount: 0, | ||
| errorUpdatedAt: 0, | ||
| fetchFailureCount: 0, | ||
| fetchFailureReason: null, | ||
| fetchMeta: null, | ||
| isInvalidated: false, | ||
| status: 'success' as const, | ||
| fetchStatus: 'idle' as const, | ||
| }, | ||
| })), | ||
| }, | ||
| } | ||
|
|
||
| storage['REACT_QUERY_OFFLINE_CACHE'] = JSON.stringify(persistedData) | ||
|
|
||
| function TestComponent() { | ||
| const combinedQueries = useQueries({ | ||
| queries: [1, 2, 3].map((id) => ({ | ||
| queryKey: ['post', id], | ||
| queryFn: () => Promise.resolve(id), | ||
| staleTime: 30_000, | ||
| })), | ||
| combine: React.useCallback( | ||
| (results: Array<QueryObserverResult<number, Error>>) => ({ | ||
| data: results.map((r) => r.data), | ||
| isPending: results.some((r) => r.isPending), | ||
| }), | ||
| [], | ||
| ), | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <div data-testid="pending">{String(combinedQueries.isPending)}</div> | ||
| <div data-testid="data"> | ||
| {combinedQueries.data.filter((d) => d !== undefined).join(',')} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const { getByTestId } = render( | ||
| <PersistQueryClientProvider | ||
| client={queryClient} | ||
| persistOptions={{ persister }} | ||
| > | ||
| <TestComponent /> | ||
| </PersistQueryClientProvider>, | ||
| ) | ||
|
|
||
| await waitFor(() => { | ||
| expect(getByTestId('pending').textContent).toBe('false') | ||
| expect(getByTestId('data').textContent).toBe('1,2,3') | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add another test that verifies that
combineisn’t called when an unrelated re-render happensThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
44bfedb
I have added the test.
Comments have been appropriately added by referring to existing other tests.