You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/framework/vue/guides/prefetching.md
+23-13Lines changed: 23 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,24 +3,30 @@ id: prefetching
3
3
title: Prefetching
4
4
---
5
5
6
-
If you're lucky enough, you may know enough about what your users will do to be able to prefetch the data they need before it's needed! If this is the case, you can use the `prefetchQuery` method to prefetch the results of a query to be placed into the cache:
6
+
If you're lucky enough, you may know enough about what your users will do to be able to prefetch the data they need before it's needed. If this is the case, use `queryClient.query` or `queryClient.infiniteQuery`to warm the cache ahead of time:
7
7
8
8
[//]: #'ExamplePrefetching'
9
9
10
10
```tsx
11
+
import { noop } from'@tanstack/vue-query'
12
+
11
13
const prefetchTodos =async () => {
12
14
// The results of this query will be cached like a normal query
13
-
awaitqueryClient.prefetchQuery({
14
-
queryKey: ['todos'],
15
-
queryFn: fetchTodos,
16
-
})
15
+
awaitqueryClient
16
+
.query({
17
+
queryKey: ['todos'],
18
+
queryFn: fetchTodos,
19
+
})
20
+
.catch(noop)
17
21
}
18
22
```
19
23
20
24
[//]: #'ExamplePrefetching'
21
25
22
26
- If **fresh** data for this query is already in the cache, the data will not be fetched
23
-
- If a `staleTime` is passed eg. `prefetchQuery({ queryKey: ['todos'], queryFn: fn, staleTime: 5000 })` and the data is older than the specified `staleTime`, the query will be fetched
27
+
- If a `staleTime` is passed e.g. `queryClient.query({ queryKey: ['todos'], queryFn: fn, staleTime: 5000 })` and the data is older than the specified `staleTime`, the query will be fetched
28
+
- As `useQuery` will retry fetches and handle errors, you can use `void` to ignore the promise from `query` and `.catch(noop)` to ignore errors.
29
+
- If you want to always return cached data when it exists, use `staleTime: 'static'`
24
30
- If no instances of `useQuery` appear for a prefetched query, it will be deleted and garbage collected after the time specified in `gcTime`.
25
31
26
32
## Prefetching Infinite Queries
@@ -30,15 +36,19 @@ Infinite Queries can be prefetched like regular Queries. Per default, only the f
30
36
[//]: #'ExampleInfiniteQuery'
31
37
32
38
```tsx
39
+
import { noop } from'@tanstack/vue-query'
40
+
33
41
const prefetchProjects =async () => {
34
42
// The results of this query will be cached like a normal query
As demonstrated, it's fine to prefetch some queries and let others fetch on the queryClient. This means you can control what content server renders or not by adding or removing `prefetchQuery` or `suspense` for a specific query.
173
+
As demonstrated, it's fine to prefetch some queries and let others fetch on the client. This means you can control what content server renders or not by adding or removing `queryClient.query` or `suspense` for a specific query.
173
174
174
175
## Using Vite SSR
175
176
@@ -237,7 +238,7 @@ Then, call VueQuery from any component using Vue's `onServerPrefetch`:
237
238
238
239
Any query with an error is automatically excluded from dehydration. This means that the default behavior is to pretend these queries were never loaded on the server, usually showing a loading state instead, and retrying the queries on the queryClient. This happens regardless of error.
239
240
240
-
Sometimes this behavior is not desirable, maybe you want to render an error page with a correct status code instead on certain errors or queries. In those cases, use `fetchQuery` and catch any errors to handle those manually.
241
+
Sometimes this behavior is not desirable, maybe you want to render an error page with a correct status code instead on certain errors or queries. In those cases, use `queryClient.query` and catch any errors to handle those manually.
241
242
242
243
### Staleness is measured from when the query was fetched on the server
0 commit comments