Skip to content

Commit a165f29

Browse files
committed
docs: add data loaders section
1 parent db2acd5 commit a165f29

File tree

8 files changed

+154
-27
lines changed

8 files changed

+154
-27
lines changed

CHANGELOG.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ internally to represent the folder structure.
107107

108108
This patch contains the necessary fixes to allow importing the data loaders. However, they cannot be imported from `vue-router/auto` nor from `unplugin-vue-router/runtime`. Instead, they should be imported from `unplugin-vue-router/data-loaders/...`. This is needed as some of the loaders depends on extra packages that not all users have installed. At the moment, there are two data loaders
109109

110-
- `unplugin-vue-router/data-loaders/basic`: https://uvr.esm.is/rfcs/data-loaders/basic.html
111-
- `unplugin-vue-router/data-loaders/pinia-colada`: https://uvr.esm.is/rfcs/data-loaders/colada.html
110+
- `unplugin-vue-router/data-loaders/basic`: https://uvr.esm.is/data-loaders/basic/
111+
- `unplugin-vue-router/data-loaders/pinia-colada`: https://uvr.esm.is/data-loaders/colada/
112112

113113
### Bug Fixes
114114

@@ -195,10 +195,10 @@ For people using the file-based routing, you now need to add `unplugin-vue-route
195195

196196
- Data Loaders have been redesigned to be more flexible
197197
and account for other libraries. Notably, the caching behavior has been
198-
moved out of the basic loader to an extended one [pinia-colada](https://uvr.esm.is/rfcs/data-loaders/colada.html) and the [basic loader](https://uvr.esm.is/rfcs/data-loaders/basic.html)
198+
moved out of the basic loader to an extended one [pinia-colada](https://uvr.esm.is/data-loaders/colada/) and the [basic loader](https://uvr.esm.is/data-loaders/basic/)
199199
has no cache. All of the pending bugs have also been fixed.
200200
I recommend you to give the RFC examples a new read to get
201-
setup: https://uvr.esm.is/rfcs/data-loaders/. Most of the changes are
201+
setup: https://uvr.esm.is/data-loaders/rfc. Most of the changes are
202202
simplifying things by removing them.
203203
Here is a list of the breaking changes to simplify
204204
migration:
@@ -207,9 +207,9 @@ For people using the file-based routing, you now need to add `unplugin-vue-route
207207
- Manual work needed to add loaders with `HasDataLoaderMeta` has been
208208
removed. It is just no longer needed. Loaders are picked up from lazy
209209
loaded components and must otherwise be directly added to a `meta.loaders`
210-
array. See the example at https://uvr.esm.is/rfcs/data-loaders/#basic-example
210+
array. See the example at https://uvr.esm.is/data-loaders/rfc.html#basic-example
211211
- The function `setupDataFetchingGuard` has been replaced with a Vue
212-
Plugin. See https://uvr.esm.is/rfcs/data-loaders/#data-loader-setup
212+
Plugin. See https://uvr.esm.is/data-loaders/rfc.html#data-loader-setup
213213
for details.
214214
- If you were relying on `cacheTime`, use the `staleTime` option in the
215215
new [`defineColadaLoader()`](https://uvr.esm.is/rfcs/data-loaders/colada) based off [@pinia/colada](https://github.com/posva/pinia-colada)

docs/.vitepress/config.ts

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default defineConfig({
132132
//
133133
sidebarGettingStarted(),
134134
sidebarGuide(),
135-
sidebarRFC(),
135+
sidebarDataLoaders(),
136136
sidebarNuxt(),
137137
],
138138
},
@@ -187,35 +187,75 @@ function sidebarGuide(): SidebarGroup {
187187
}
188188
}
189189

190-
function sidebarNuxt(): SidebarGroup {
190+
function sidebarDataLoaders(): SidebarGroup {
191191
return {
192192
collapsed: false,
193-
text: 'Nuxt',
193+
text: 'Data Loaders',
194194
items: [
195195
{
196-
text: 'Getting Started',
197-
link: '/nuxt/getting-started',
196+
text: 'Introduction',
197+
link: '/data-loaders/',
198+
},
199+
{
200+
text: 'Defining Loader',
201+
link: '/data-loaders/introduction',
202+
},
203+
{
204+
text: 'Cancelling a load',
205+
link: '/data-loaders/load-cancellation',
206+
},
207+
{
208+
text: 'Reloading data',
209+
link: '/data-loaders/reloading-data',
210+
},
211+
{
212+
text: 'Navigation Aware',
213+
link: '/data-loaders/navigation-aware',
214+
},
215+
{
216+
text: 'Organizing Loaders',
217+
link: '/data-loaders/organization',
218+
},
219+
{
220+
text: 'Error Handling',
221+
link: '/data-loaders/error-handling',
222+
},
223+
{
224+
text: 'Nested Loaders',
225+
link: '/data-loaders/nested-loaders',
226+
},
227+
{
228+
text: 'SSR',
229+
link: '/data-loaders/ssr',
230+
},
231+
232+
// loaders
233+
{
234+
text: 'Basic Loader',
235+
link: '/data-loaders/basic/',
236+
},
237+
{
238+
text: 'Colada Loader',
239+
link: '/data-loaders/colada/',
240+
},
241+
242+
// last
243+
{
244+
text: 'RFC',
245+
link: '/data-loaders/rfc',
198246
},
199247
],
200248
}
201249
}
202250

203-
function sidebarRFC(): SidebarGroup {
251+
function sidebarNuxt(): SidebarGroup {
204252
return {
205253
collapsed: false,
206-
text: 'RFC',
254+
text: 'Nuxt',
207255
items: [
208256
{
209-
text: 'Data Loaders',
210-
link: '/rfcs/data-loaders/',
211-
},
212-
{
213-
text: 'Pinia Colada loader',
214-
link: '/rfcs/data-loaders/colada',
215-
},
216-
{
217-
text: 'Basic Loader',
218-
link: '/rfcs/data-loaders/basic',
257+
text: 'Getting Started',
258+
link: '/nuxt/getting-started',
219259
},
220260
],
221261
}

docs/.vitepress/theme/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { EnhanceAppContext } from 'vitepress'
66
export default {
77
extends: Theme,
88
enhanceApp({ app }: EnhanceAppContext) {
9-
// @ts-expect-error: bugged?
109
app.use(TwoslashFloatingVue)
1110
},
1211
}
File renamed without changes.
File renamed without changes.

docs/data-loaders/index.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,93 @@
11
# Data Loaders
22

3+
Data loaders streamline any asynchronous state management with Vue Router, like **Data Fetching**. Adopting Data loaders ensures a consistent and efficient way to manage data fetching in your application. Keep all the benefits of using libraries like [Pinia Colada](./colada/) or [Apollo](./apollo/) and integrate them seamlessly with client-side navigation.
4+
5+
This is achieved by extracting the loading logic **outside** of the component `setup` (unlike `<Suspense>`). This way, the loading logic can be executed independently of the component lifecycle, and the component can focus on rendering the data. Data Loaders are automatically collected and awaited within a navigation guard, ensuring the data is ready before rendering the component.
6+
7+
## Features
8+
9+
- Parallel data fetching and deduplication
10+
- Automatic loading state management
11+
- Error handling
12+
- Extensible by loader implementations
13+
- SSR support
14+
- Prefetching data support
15+
316
## Installation
417

18+
After installing [unplugin-vue-router](../introduction.md), install the `DataLoaderPlugin` **before the `router`**.
19+
20+
```ts{12-15} twoslash
21+
import 'unplugin-vue-router/client'
22+
import './typed-router.d'
23+
// @moduleResolution: bundler
24+
// ---cut---
25+
import { createApp } from 'vue'
26+
import { routes } from 'vue-router/auto-routes'
27+
import { createRouter, createWebHistory } from 'vue-router'
28+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders' // [!code ++]
29+
30+
const router = createRouter({
31+
history: createWebHistory(),
32+
routes,
33+
})
34+
35+
const app = createApp({})
36+
// Register the plugin before the router
37+
app.use(DataLoaderPlugin, { router }) // [!code ++]
38+
// adding the router will trigger the initial navigation
39+
app.use(router)
40+
app.mount('#app')
41+
```
42+
543
## Quick start
44+
45+
There are different data loaders implementation, the most simple one is the [Basic Loader](./basic/) which always reruns data fetching. A more efficient one, is the [Colada Loader](./colada/) which uses [@pinia/colada](https://github.com/posva/pinia-colada) under the hood. In the following examples, we will be using the _basic loader_.
46+
47+
Loaders are [composables](https://vuejs.org/guide/reusability/composables.html) defined through a `defineLoader` function like `defineBasicLoader` or `defineColadaLoader`. They are _used_ in the component `setup` to extract the needed information.
48+
49+
To get started, _define_ and _export_ a loader from a page:
50+
51+
```vue{2,5-7,11-16} twoslash
52+
<script lang="ts">
53+
import 'unplugin-vue-router/client'
54+
import './typed-router.d'
55+
// ---cut---
56+
import { defineBasicLoader } from 'unplugin-vue-router/data-loaders/basic'
57+
import { getUserById } from '../api'
58+
59+
export const useUserData = defineBasicLoader('/users/[id]', async (route) => {
60+
return getUserById(route.params.id)
61+
})
62+
</script>
63+
64+
<script setup lang="ts">
65+
const {
66+
data: user, // the data returned by the loader
67+
isLoading, // a boolean indicating if the loader is fetching data
68+
error, // an error object if the loader failed
69+
reload, // a function to refetch the data without navigating
70+
} = useUserData()
71+
</script>
72+
73+
<template>
74+
<main>
75+
<p v-if="isLoading">Loading...</p>
76+
<template v-else-if="error">
77+
<p>{{ error.message }}</p>
78+
<button @click="reload()">Retry</button>
79+
</template>
80+
<template v-else>
81+
<p>{{ user }}</p>
82+
</template>
83+
</main>
84+
</template>
85+
```
86+
87+
The loader will automatically run when the route changes, for example when navigating to `/users/1`, even when coming from `/users/2`, the loader will fetch the data and delay the navigation until the data is ready.
88+
89+
On top of that, you are free to _reuse_ the returned composable `useUserData` in any other component, and it will automatically share the same data fetching instance.
90+
91+
## Why Data Loaders?
92+
93+
Data fetching is the most common need for a web application. There are many ways of handling data fetching, and they all have their pros and cons. Data loaders are a way to streamline data fetching in your application. Instead of forcing you to choose between different libraries, data loaders provide a consistent way to manage data fetching in your application no matter the underlying library or strategy you use.
File renamed without changes.

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ hero:
1212
text: Get Started
1313
link: /introduction
1414
- theme: alt
15-
text: Data Loaders RFC
16-
link: /rfcs/data-loaders/
15+
text: Data Loaders
16+
link: /data-loaders/
1717

1818
features:
1919
- title: Type Safe
@@ -30,5 +30,5 @@ features:
3030
- title: Data Loaders
3131
icon: 🔄
3232
details: Support for the upcoming Data Loaders for Vue Router.
33-
link: /rfcs/data-loaders/
33+
link: /data-loaders/
3434
---

0 commit comments

Comments
 (0)