-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(query-core): implement client-informed server prefetching #10023
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1231d8
feat(query-core): implement client-informed server prefetching
ayden94 dbab049
test(query-core): add verification for client cache state prefetching
ayden94 abebeb4
feat(query-core): optimize prefetch logic and support infinite queries
ayden94 ad34eca
chore: add changeset for client-informed prefetching
ayden94 49be237
feat(query-core): add extractClientCacheState helper to QueryCache
ayden94 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
62 changes: 62 additions & 0 deletions
62
packages/query-core/src/__tests__/clientCacheState.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,62 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { QueryClient } from '../queryClient' | ||
| import { sleep } from '../utils' | ||
|
|
||
| describe('Client Cache State', () => { | ||
| it('should prefetch query normally when no client state is provided', async () => { | ||
| const queryClient = new QueryClient() | ||
| const key = ['test'] | ||
| const serverData = 'server-data' | ||
| let fetchCount = 0 | ||
|
|
||
| const queryFn = async () => { | ||
| fetchCount++ | ||
| await sleep(10) | ||
| return serverData | ||
| } | ||
|
|
||
| await queryClient.prefetchQuery({ | ||
| queryKey: key, | ||
| queryFn, | ||
| staleTime: 5000, | ||
| }) | ||
|
|
||
| expect(fetchCount).toBe(1) | ||
| }) | ||
|
|
||
| // This test describes the DESIRED behavior, which is currently NOT implemented. | ||
| // It is expected to FAIL until we implement the changes. | ||
| it('should SKIP prefetch when client has fresh data (Simulated)', async () => { | ||
| // 1. Simulate hypothetical QueryClient with clientCacheState | ||
| // We haven't implemented the types yet, so we cast to any or expect it to be ignored for now. | ||
| const clientCacheState = { | ||
| '["test-optim"]': Date.now(), // Client has fresh data right now | ||
| } | ||
|
|
||
| // @ts-ignore - API not implemented yet | ||
| const queryClient = new QueryClient({ clientCacheState }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| const key = ['test-optim'] | ||
| const serverData = 'server-data' | ||
| let fetchCount = 0 | ||
|
|
||
| const queryFn = async () => { | ||
| fetchCount++ | ||
| await sleep(10) | ||
| return serverData | ||
| } | ||
|
|
||
| await queryClient.prefetchQuery({ | ||
| queryKey: key, | ||
| queryFn, | ||
| staleTime: 5000, // 5 seconds stale time | ||
| }) | ||
|
|
||
| // CURRENT BEHAVIOR: fetchCount is 1 (Server fetches anyway) | ||
| // DESIRED BEHAVIOR: fetchCount should be 0 (Server skips because client has it) | ||
|
|
||
| // We expect this to equal 0 if our feature is working. | ||
| // For now, let's assert 0 and see it fail, proving the need for the feature. | ||
| expect(fetchCount).toBe(0) | ||
| }) | ||
| }) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.