Skip to content

Commit 163d205

Browse files
committed
docs(vue-query): docs for new impertive methods for Vue
1 parent 18adc15 commit 163d205

2 files changed

Lines changed: 29 additions & 18 deletions

File tree

docs/framework/vue/guides/prefetching.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@ id: prefetching
33
title: Prefetching
44
---
55

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:
77

88
[//]: # 'ExamplePrefetching'
99

1010
```tsx
11+
import { noop } from '@tanstack/vue-query'
12+
1113
const prefetchTodos = async () => {
1214
// The results of this query will be cached like a normal query
13-
await queryClient.prefetchQuery({
14-
queryKey: ['todos'],
15-
queryFn: fetchTodos,
16-
})
15+
await queryClient
16+
.query({
17+
queryKey: ['todos'],
18+
queryFn: fetchTodos,
19+
})
20+
.catch(noop)
1721
}
1822
```
1923

2024
[//]: # 'ExamplePrefetching'
2125

2226
- 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'`
2430
- If no instances of `useQuery` appear for a prefetched query, it will be deleted and garbage collected after the time specified in `gcTime`.
2531

2632
## Prefetching Infinite Queries
@@ -30,15 +36,19 @@ Infinite Queries can be prefetched like regular Queries. Per default, only the f
3036
[//]: # 'ExampleInfiniteQuery'
3137

3238
```tsx
39+
import { noop } from '@tanstack/vue-query'
40+
3341
const prefetchProjects = async () => {
3442
// The results of this query will be cached like a normal query
35-
await queryClient.prefetchInfiniteQuery({
36-
queryKey: ['projects'],
37-
queryFn: fetchProjects,
38-
initialPageParam: 0,
39-
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
40-
pages: 3, // prefetch the first 3 pages
41-
})
43+
await queryClient
44+
.infiniteQuery({
45+
queryKey: ['projects'],
46+
queryFn: fetchProjects,
47+
initialPageParam: 0,
48+
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
49+
pages: 3, // prefetch the first 3 pages
50+
})
51+
.catch(noop)
4252
}
4353
```
4454

docs/framework/vue/guides/ssr.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ export default defineNuxtPlugin((nuxt) => {
5050

5151
Now you are ready to prefetch some data in your pages with `onServerPrefetch`.
5252

53-
- Prefetch all the queries that you need with `queryClient.prefetchQuery` or `suspense`
53+
- Prefetch all the queries that you need with `queryClient.query`, `queryClient.infiniteQuery`, or `suspense`
5454

5555
```ts
5656
export default defineComponent({
5757
setup() {
58-
const { data, suspense } = useQuery({
58+
const queryClient = useQueryClient()
59+
const { data } = useQuery({
5960
queryKey: ['test'],
6061
queryFn: fetcher,
6162
})
@@ -110,7 +111,7 @@ Now you are ready to prefetch some data in your pages with `onServerPrefetch`.
110111

111112
- Use `useContext` to get nuxt context
112113
- Use `useQueryClient` to get server-side instance of `queryClient`
113-
- Prefetch all the queries that you need with `queryClient.prefetchQuery` or `suspense`
114+
- Prefetch all the queries that you need with `queryClient.query`, `queryClient.infiniteQuery`, or `suspense`
114115
- Dehydrate `queryClient` to the `nuxtContext`
115116

116117
```vue
@@ -169,7 +170,7 @@ export default defineComponent({
169170
</script>
170171
```
171172

172-
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.
173174

174175
## Using Vite SSR
175176

@@ -237,7 +238,7 @@ Then, call VueQuery from any component using Vue's `onServerPrefetch`:
237238
238239
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.
239240
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.
241242
242243
### Staleness is measured from when the query was fetched on the server
243244

0 commit comments

Comments
 (0)