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
{{ message }}
Repository navigation
Commit ee3cf31
Browse filesBrowse the repository at this point in the historyBrowse files
feat(data-loaders)!: define colada loaders with query options (#2973)
BREAKING CHANGE: `defineColadaLoader()` now takes a function that returns Pinia Colada query options. Loader options (`lazy`, `server`, `errors`) move to the last argument. The `signal` passed to `query` is now the one from Pinia Colada. The navigation signal is in the second argument of the function.
```ts
// before
defineColadaLoader('/users/[id]', {
key: to => ['users', to.params.id],
query: (to, { signal }) => getUser(to.params.id, { signal }),
staleTime: 10_000,
lazy: true,
})
// after
defineColadaLoader(
'/users/[id]',
to => ({
key: ['users', to.params.id],
query: ({ signal }) => getUser(to.params.id, { signal }),
staleTime: 10_000,
}),
{ lazy: true }
)
```
Copy file name to clipboardExpand all lines: packages/docs/data-loaders/colada/index.md
+53-12Lines changed: 53 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Loaders that use [@pinia/colada](https://github.com/posva/pinia-colada) under the hood. These loaders provide the most efficient solution to asynchronous state with cache, ssr support and more.
4
4
5
-
The key used in these loaders is directly passed to `useQuery()` from `@pinia/colada` and can be invalidated with `useMutation()` hooks. See [Pinia Colada documentation about query invalidation](https://pinia-colada.esm.dev/guide/query-invalidation.html) to learn more.
5
+
A Pinia Colada loader is a `useQuery()` bound to the route: you give it a function that receives the route and returns [query options](https://pinia-colada.esm.dev/guide/queries.html). The key is directly passed to `useQuery()` from `@pinia/colada` and can be invalidated with `useMutation()` hooks. See [Pinia Colada documentation about query invalidation](https://pinia-colada.esm.dev/guide/query-invalidation.html) to learn more.
6
6
7
7
## Setup
8
8
@@ -23,14 +23,12 @@ import './typed-router.d'
23
23
import { defineColadaLoader } from 'vue-router/experimental/pinia-colada'
24
24
import { getUserById } from '../api'
25
25
26
-
export const useUserData = defineColadaLoader({
27
-
async query(to, { signal }) {
28
-
return getUserById(to.params.id, { signal })
29
-
},
30
-
key: to => ['users', to.params.id],
26
+
export const useUserData = defineColadaLoader('/users/[id]', to => ({
27
+
key: ['users', to.params.id],
28
+
query: ({ signal }) => getUserById(to.params.id, { signal }),
31
29
// Keep the data "fresh" 10 seconds to avoid fetching the same data too often
32
30
staleTime: 10000,
33
-
})
31
+
}))
34
32
</script>
35
33
36
34
<script lang="ts" setup>
@@ -82,14 +80,57 @@ const {
82
80
<!-- TODO: add autocompletion to show all pinia colada options -->
83
81
84
82
::: tip
85
-
You can pass a route name to `defineColadaLoader` to get typed routes in the `query` function.
83
+
The route name is optional. It gives a typed `to` parameter.
84
+
:::
85
+
86
+
## Reusing query options
87
+
88
+
Because the loader returns query options, you can reuse the ones defined with [`defineQueryOptions()`](https://pinia-colada.esm.dev/guide/reusable-queries.html):
Data loader options like `lazy`, `server` or `errors` go in the last argument. They are not query options:
107
+
108
+
```ts
109
+
exportconst useUserData =defineColadaLoader(
110
+
'/users/[id]',
111
+
to=>userQuery(to.params.id),
112
+
{ lazy: true }
113
+
)
114
+
```
115
+
116
+
## Abort signals
117
+
118
+
The `query` function receives the `signal` of Pinia Colada. Like with `useQuery()`, Pinia Colada aborts it when no component uses the query anymore, e.g. when a cancelled navigation makes the page show the previous query again.
119
+
120
+
The function that returns the query options receives the navigation context as its second argument. Its `signal` is aborted when the navigation is cancelled. Use it only if the request must stop with the navigation:
The function that returns the query options can run more than once for the same route. Do not cause side effects in it.
93
134
:::
94
135
95
136
## Refresh by default
@@ -98,7 +139,7 @@ To avoid unnecessary frequent refreshes, Pinia Colada refreshes the data when na
98
139
99
140
## Route tracking
100
141
101
-
The `query` function tracks what is used in the `to` parameterand will only refresh the data if **tracked** properties change. This means that if you use `to.params.id` in the `query` function, it will only refetch the data if the `id` parameter changes but not if other properties like `to.query`, `to.hash` or even `to.params.other` change. To make sure the data is updated, it will still refresh in these scenarios. Configure the `staleTime` option to control how often the data should be refreshed.
142
+
The loader tracks what is used in the `to` parameter, in the function that returns the query options and in the `query` function. It only refreshes the data if **tracked** properties change. This means that if you use `to.params.id`, it will only refetch the data if the `id` parameter changes but not if other properties like `to.query`, `to.hash` or even `to.params.other` change. To make sure the data is updated, it will still refresh in these scenarios. Configure the `staleTime` option to control how often the data should be refreshed.
Copy file name to clipboardExpand all lines: packages/docs/data-loaders/defining-loaders.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Defining Data Loaders
2
2
3
-
In order to use data loaders, you need to define them first. Data loaders themselves are the composables returned by the different `defineLoader` functions. Each loader definition is specific to the `defineLoader` function used. For example, `defineBasicLoader` expects an async function as the first argument while `defineColadaLoader` expects an object with a `query` function. All loaders should allow to pass an async function that can throw errors, and call `reroute()` to control the navigation.
3
+
In order to use data loaders, you need to define them first. Data loaders themselves are the composables returned by the different `defineLoader` functions. Each loader definition is specific to the `defineLoader` function used. For example, `defineBasicLoader` expects an async function as the first argument while `defineColadaLoader` expects a function that returns Pinia Colada query options. All loaders should allow to pass an async function that can throw errors, and call `reroute()` to control the navigation.
4
4
5
5
Any composables returned by _any_`defineLoader` function share the same signature:
0 commit comments