-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(vue-query): add usePrefetchQuery and usePrefetchInfiniteQuery #10372
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
DamianOsipiuk
merged 6 commits into
TanStack:main
from
thalassophilia:feat/vue-prefetch-composables
Apr 11, 2026
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9311c3f
feat(vue-query): add prefetch composables
thalassophilia a72cdb5
docs(vue-query): document prefetch composables
thalassophilia 1f1c4a7
Merge branch 'main' into feat/vue-prefetch-composables
thalassophilia 9af0695
Merge branch 'main' into feat/vue-prefetch-composables
DamianOsipiuk 3b58be0
revert unrelated change
DamianOsipiuk 193e183
ci: apply automated fixes
autofix-ci[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/vue-query': minor | ||
| --- | ||
|
|
||
| Add usePrefetchQuery and usePrefetchInfiniteQuery to vue-query. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: usePrefetchInfiniteQuery | ||
| title: usePrefetchInfiniteQuery | ||
| ref: docs/framework/react/reference/usePrefetchInfiniteQuery.md | ||
| replace: { '@tanstack/react-query': '@tanstack/vue-query' } | ||
| --- |
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,6 @@ | ||
| --- | ||
| id: usePrefetchQuery | ||
| title: usePrefetchQuery | ||
| ref: docs/framework/react/reference/usePrefetchQuery.md | ||
| replace: { '@tanstack/react-query': '@tanstack/vue-query' } | ||
| --- |
96 changes: 96 additions & 0 deletions
96
packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test-d.ts
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,96 @@ | ||
| import { assertType, describe, expectTypeOf, it } from 'vitest' | ||
| import { ref } from 'vue-demi' | ||
| import { skipToken } from '@tanstack/query-core' | ||
| import { usePrefetchInfiniteQuery } from '..' | ||
|
|
||
| describe('usePrefetchInfiniteQuery', () => { | ||
| it('should return nothing', () => { | ||
| const result = usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => 1, | ||
| }) | ||
|
|
||
| expectTypeOf(result).toEqualTypeOf<void>() | ||
| }) | ||
|
|
||
| it('should require initialPageParam and getNextPageParam', () => { | ||
| assertType( | ||
| // @ts-expect-error TS2345 | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('should not allow refetchInterval, enabled or throwOnError options', () => { | ||
| assertType( | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => 1, | ||
| // @ts-expect-error TS2353 | ||
| refetchInterval: 1000, | ||
| }), | ||
| ) | ||
|
|
||
| assertType( | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => 1, | ||
| // @ts-expect-error TS2353 | ||
| enabled: true, | ||
| }), | ||
| ) | ||
|
|
||
| assertType( | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => 1, | ||
| // @ts-expect-error TS2353 | ||
| throwOnError: true, | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('should accept refs in infinite query options', () => { | ||
| assertType( | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key', ref('id')], | ||
| queryFn: () => Promise.resolve(5), | ||
| initialPageParam: ref(1), | ||
| getNextPageParam: () => 1, | ||
| staleTime: ref(1000), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('should not allow skipToken in queryFn', () => { | ||
| assertType( | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => 1, | ||
| // @ts-expect-error | ||
| queryFn: skipToken, | ||
| }), | ||
| ) | ||
|
|
||
| assertType( | ||
| usePrefetchInfiniteQuery({ | ||
| queryKey: ['key'], | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => 1, | ||
| // @ts-expect-error | ||
| queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5), | ||
| }), | ||
| ) | ||
| }) | ||
| }) |
157 changes: 157 additions & 0 deletions
157
packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test.ts
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,157 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' | ||
| import { nextTick, ref } from 'vue-demi' | ||
| import { QueryClient } from '../queryClient' | ||
| import { usePrefetchInfiniteQuery } from '../usePrefetchInfiniteQuery' | ||
|
|
||
| describe('usePrefetchInfiniteQuery', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| test('should prefetch infinite query if query state does not exist', () => { | ||
| const queryClient = new QueryClient() | ||
| const prefetchInfiniteQuerySpy = vi.spyOn( | ||
| queryClient, | ||
| 'prefetchInfiniteQuery', | ||
| ) | ||
| const queryFn = vi.fn(() => | ||
| Promise.resolve({ data: 'prefetched', currentPage: 1 }), | ||
| ) | ||
|
|
||
| usePrefetchInfiniteQuery( | ||
| { | ||
| queryKey: ['prefetch-infinite-query'], | ||
| queryFn, | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => undefined, | ||
| }, | ||
| queryClient, | ||
| ) | ||
|
|
||
| expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1) | ||
| expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({ | ||
| queryKey: ['prefetch-infinite-query'], | ||
| queryFn, | ||
| initialPageParam: 1, | ||
| getNextPageParam: expect.any(Function), | ||
| }) | ||
| }) | ||
|
|
||
| test('should not prefetch infinite query if query state exists', () => { | ||
| const queryClient = new QueryClient() | ||
| const prefetchInfiniteQuerySpy = vi.spyOn( | ||
| queryClient, | ||
| 'prefetchInfiniteQuery', | ||
| ) | ||
| const queryFn = vi.fn(() => | ||
| Promise.resolve({ data: 'prefetched', currentPage: 1 }), | ||
| ) | ||
|
|
||
| queryClient.setQueryData(['prefetch-infinite-query-existing'], { | ||
| pages: [{ data: 'existing', currentPage: 1 }], | ||
| pageParams: [1], | ||
| }) | ||
|
|
||
| usePrefetchInfiniteQuery( | ||
| { | ||
| queryKey: ['prefetch-infinite-query-existing'], | ||
| queryFn, | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => undefined, | ||
| }, | ||
| queryClient, | ||
| ) | ||
|
|
||
| expect(prefetchInfiniteQuerySpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('should unwrap refs in infinite query options', () => { | ||
| const queryClient = new QueryClient() | ||
| const prefetchInfiniteQuerySpy = vi.spyOn( | ||
| queryClient, | ||
| 'prefetchInfiniteQuery', | ||
| ) | ||
| const nestedRef = ref('value') | ||
|
|
||
| usePrefetchInfiniteQuery( | ||
| { | ||
| queryKey: ['prefetch-infinite-query-ref', nestedRef], | ||
| queryFn: () => Promise.resolve({ data: 'prefetched', currentPage: 1 }), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => undefined, | ||
| }, | ||
| queryClient, | ||
| ) | ||
|
|
||
| expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| queryKey: ['prefetch-infinite-query-ref', 'value'], | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('should prefetch infinite query again when query key changes reactively', async () => { | ||
| const queryClient = new QueryClient() | ||
| const prefetchInfiniteQuerySpy = vi.spyOn( | ||
| queryClient, | ||
| 'prefetchInfiniteQuery', | ||
| ) | ||
| const keyRef = ref('first') | ||
|
|
||
| usePrefetchInfiniteQuery( | ||
| () => ({ | ||
| queryKey: ['prefetch-infinite-query-reactive', keyRef.value], | ||
| queryFn: () => Promise.resolve({ data: keyRef.value, currentPage: 1 }), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => undefined, | ||
| }), | ||
| queryClient, | ||
| ) | ||
|
|
||
| expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1) | ||
| expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ | ||
| queryKey: ['prefetch-infinite-query-reactive', 'first'], | ||
| }), | ||
| ) | ||
|
|
||
| keyRef.value = 'second' | ||
| await nextTick() | ||
|
|
||
| expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(2) | ||
| expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ | ||
| queryKey: ['prefetch-infinite-query-reactive', 'second'], | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| test('should warn when used outside of setup function in development mode', () => { | ||
| vi.stubEnv('NODE_ENV', 'development') | ||
| const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
|
|
||
| try { | ||
| usePrefetchInfiniteQuery( | ||
| { | ||
| queryKey: ['outside-scope-prefetch-infinite-query'], | ||
| queryFn: () => | ||
| Promise.resolve({ data: 'prefetched', currentPage: 1 }), | ||
| initialPageParam: 1, | ||
| getNextPageParam: () => undefined, | ||
| }, | ||
| new QueryClient(), | ||
| ) | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.', | ||
| ) | ||
| } finally { | ||
| warnSpy.mockRestore() | ||
| vi.unstubAllEnvs() | ||
| } | ||
| }) | ||
| }) |
72 changes: 72 additions & 0 deletions
72
packages/vue-query/src/__tests__/usePrefetchQuery.test-d.ts
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,72 @@ | ||
| import { assertType, describe, expectTypeOf, it } from 'vitest' | ||
| import { ref } from 'vue-demi' | ||
| import { skipToken } from '@tanstack/query-core' | ||
| import { usePrefetchQuery } from '..' | ||
|
|
||
| describe('usePrefetchQuery', () => { | ||
| it('should return nothing', () => { | ||
| const result = usePrefetchQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| }) | ||
|
|
||
| expectTypeOf(result).toEqualTypeOf<void>() | ||
| }) | ||
|
|
||
| it('should not allow refetchInterval, enabled or throwOnError options', () => { | ||
| assertType( | ||
| usePrefetchQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| // @ts-expect-error TS2353 | ||
| refetchInterval: 1000, | ||
| }), | ||
| ) | ||
|
|
||
| assertType( | ||
| usePrefetchQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| // @ts-expect-error TS2353 | ||
| enabled: true, | ||
| }), | ||
| ) | ||
|
|
||
| assertType( | ||
| usePrefetchQuery({ | ||
| queryKey: ['key'], | ||
| queryFn: () => Promise.resolve(5), | ||
| // @ts-expect-error TS2353 | ||
| throwOnError: true, | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('should accept refs in query options', () => { | ||
| assertType( | ||
| usePrefetchQuery({ | ||
| queryKey: ['key', ref('id')], | ||
| queryFn: () => Promise.resolve(5), | ||
| staleTime: ref(1000), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('should not allow skipToken in queryFn', () => { | ||
| assertType( | ||
| usePrefetchQuery({ | ||
| queryKey: ['key'], | ||
| // @ts-expect-error | ||
| queryFn: skipToken, | ||
| }), | ||
| ) | ||
|
|
||
| assertType( | ||
| usePrefetchQuery({ | ||
| queryKey: ['key'], | ||
| // @ts-expect-error | ||
| queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5), | ||
| }), | ||
| ) | ||
| }) | ||
| }) |
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.
Seems to be unrelated chanage. Please revert