Skip to content

Commit 9f462c9

Browse files
committed
docs: organizing loaders
1 parent a46feb2 commit 9f462c9

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

docs/data-loaders/defining-loaders.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,7 @@ The router needs to know what loaders should be ran with which page. This is ach
320320
</script>
321321
<!-- ...rest of the component -->
322322
```
323+
324+
### _Disconnecting_ a loader from a page
325+
326+
It is also possible **not to connect a loader to a page**. This allows you to delay the loading until the component is mounted. Usually you want to start loading the data as soon as possible but in some cases, it might be better to wait until the component is mounted. This can be achieved by not exporting the loader from the page component.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Cancelling a data loader

docs/data-loaders/navigation-aware.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
## Loading after the navigation
1010

11-
It's possible to not start loading the data until the navigation is done. To do this, simply **do not attach the loader to the page**. It will eventually start loading when the page is mounted.
11+
It's possible to not start loading the data until the navigation is done. To do this, simply [**do not attach the loader to the page**](./defining-loaders.md#disconnecting-a-loader-from-a-page). It will eventually start loading when the page is mounted.

docs/data-loaders/organization.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
11
# Loaders Organization
22

3+
While most examples show loaders defined in the same file as the page component, it's possible to define them in separate files and import them in the page component. This flexibility allows you to control not only the codebase organization but also **how chunks are split**.
4+
5+
If a loader is used in multiple pages, it might be a better idea to extract it to a separate file instead of exporting it in one page and importing it in the others. This is because pages importing it will usually load the whole page component chunk in order to get the loader.
6+
7+
::: code-group
8+
9+
```ts [loaders/issues.ts]
10+
import { defineBasicLoader } from 'unplugin-vue-router/data-loaders/basic'
11+
import { getIssuesByProjectId } from '@/api'
12+
13+
export const useProjectIssues = defineBasicLoader('/[projectId]/issues', (to) =>
14+
getIssuesByProjectId(to.params.projectId)
15+
)
16+
```
17+
18+
```vue{2-3,7} [pages/[projectId]/issues.vue]
19+
<script lang="ts">
20+
import { useProjectIssues } from '@/loaders/issues'
21+
export { useProjectIssues }
22+
</script>
23+
24+
<script setup lang="ts">
25+
const { data: issues } = useProjectIssues()
26+
</script>
27+
```
28+
29+
```vue{2,4,9} [pages/[projectId]/insights.vue]
30+
<script lang="ts">
31+
import { useProjectIssues } from '@/loaders/issues'
32+
import { useProjectPullRequests } from '@/loaders/pull-requests'
33+
export { useProjectIssues, useProjectPullRequests }
34+
</script>
35+
36+
<script setup lang="ts">
37+
const { data: issues } = useProjectIssues()
38+
const { data: pullRequests } = useProjectPullRequests()
39+
</script>
40+
```
41+
42+
:::
43+
44+
In the example above, the `useProjectIssues` loader is defined in a separate file and imported in two different pages, `pages/[projectId]/issues.vue` and `pages/[projectId]/insights.vue`. They both use the same data but present it in a different way so there is no reason to create two different loaders for issues. By extracting the loader into a separate file, we ensure an optimal chunk split.
45+
46+
When using this pattern, remember to **export the loader** in all the page components that use it. This is what allows the router to await the loader before rendering the page.
47+
348
## Usage outside of page components
49+
50+
Until now, we have only seen loaders used in page components. However, one of the benefits of using loaders is that they can be **reused in many part of your application**, just like regular composables. This will not only eliminate code duplication but also ensure an optimal and performant data fetching by **deduplicating requests and sharing the data**.
51+
52+
To use a loader outside of a page component, you can simply **import it** and use it like any other composable, without the need to export it.
53+
54+
```vue
55+
<script setup lang="ts">
56+
// You can even import it from the page component
57+
import { useProjectIssues } from '@/pages/[projectId]/issues.vue'
58+
59+
const { data: issues } = useProjectIssues()
60+
</script>
61+
```
62+
63+
## Nested Routes
64+
65+
When defining nested routes, you don't need to worry about exporting the loader in both the parent and the child components. This will be automatically optimized for you and the loader will be shared between the parent and the child components.
66+
Because of this, it's simpler to **always export data loaders** in the page component where **they are used**.

0 commit comments

Comments
 (0)