Skip to content

Commit cef504e

Browse files
committed
docs: improve asyncData speed
1 parent a440e2f commit cef504e

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

docs/app/app.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const appConfig = useAppConfig()
77
const colorMode = useColorMode()
88
99
const { data: navigation } = await useAsyncData('navigation', () => queryCollectionNavigation('docs'))
10-
const { data: files } = await useAsyncData('search', () => queryCollectionSearchSections('docs'))
10+
const { data: files } = useLazyAsyncData('search', () => queryCollectionSearchSections('docs'), {
11+
server: false,
12+
})
1113
1214
const searchTerm = ref('')
1315

docs/app/pages/docs/[...slug].vue

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,43 @@
22
import { findPageHeadline } from '#ui-pro/utils/content'
33
44
const route = useRoute()
5+
const navigation = inject('navigation')
56
67
definePageMeta({
78
layout: 'docs',
89
})
910
10-
const { data: page } = await useAsyncData(route.path, () => queryCollection('docs').path(route.path).first())
11-
if (!page.value) {
11+
const { data } = await useAsyncData(route.path, () => Promise.all([
12+
queryCollection('docs').path(route.path).first(),
13+
queryCollectionItemSurroundings('docs', route.path, {
14+
fields: ['title', 'description'],
15+
}),
16+
]), {
17+
transform: ([page, surround]) => ({ page, surround }),
18+
})
19+
if (!data.value) {
1220
throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
1321
}
1422
15-
const { data: surround } = await useAsyncData(`${route.path}-surround`, () => {
16-
return queryCollectionItemSurroundings('docs', route.path, {
17-
fields: ['title', 'description'],
18-
})
19-
}, { default: () => [] })
20-
21-
const headline = computed(() => findPageHeadline(page.value))
23+
const headline = computed(() => findPageHeadline(navigation.value, data.value.page))
2224
2325
useSeoMeta({
2426
titleTemplate: '%s - Nuxt Content v3',
25-
title: page.value.navigation?.title || page.value.title,
26-
ogTitle: `${page.value.navigation?.title || page.value.title} - Nuxt Content v3`,
27-
description: page.value.description,
28-
ogDescription: page.value.description,
27+
title: data.value.page.navigation?.title || data.value.page.title,
28+
ogTitle: `${data.value.page.navigation?.title || data.value.page.title} - Nuxt Content v3`,
29+
description: data.value.page.description,
30+
ogDescription: data.value.page.description,
2931
})
3032
3133
defineOgImageComponent('Docs', {
3234
headline: headline.value,
33-
title: page.value.title,
35+
title: data.value.page.title,
3436
})
3537
3638
const communityLinks = computed(() => [{
3739
icon: 'i-lucide-pencil',
3840
label: 'Edit this page',
39-
to: `https://github.com/nuxt/content/edit/v3/docs/content/${page?.value?.stem}.${page.value?.extension}`,
41+
to: `https://github.com/nuxt/content/edit/v3/docs/content/${data.value.page.stem}.${data.value.page.extension}`,
4042
target: '_blank',
4143
}, {
4244
icon: 'i-lucide-star',
@@ -47,43 +49,43 @@ const communityLinks = computed(() => [{
4749
</script>
4850

4951
<template>
50-
<UPage v-if="page">
52+
<UPage v-if="data.page">
5153
<UPageHeader
52-
:title="page.title"
53-
:links="page.links"
54+
:title="data.page.title"
55+
:links="data.page.links"
5456
:headline="headline"
5557
>
5658
<template #description>
5759
<MDC
58-
v-if="page.description"
59-
:value="page.description"
60+
v-if="data.page.description"
61+
:value="data.page.description"
6062
unwrap="p"
6163
/>
6264
</template>
6365
</UPageHeader>
6466

6567
<UPageBody>
6668
<ContentRenderer
67-
v-if="page.body"
68-
:value="page"
69+
v-if="data.page.body"
70+
:value="data.page"
6971
/>
7072

7173
<USeparator />
7274

73-
<UContentSurround :surround="(surround as any)" />
75+
<UContentSurround :surround="(data.surround as any)" />
7476
</UPageBody>
7577

7678
<template
77-
v-if="page?.body?.toc?.links?.length"
79+
v-if="data.page?.body?.toc?.links?.length"
7880
#right
7981
>
8082
<UContentToc
81-
:links="page.body.toc.links"
83+
:links="data.page.body.toc.links"
8284
class="z-[2]"
8385
>
8486
<template #bottom>
8587
<USeparator
86-
v-if="page.body?.toc?.links?.length"
88+
v-if="data.page.body?.toc?.links?.length"
8789
type="dashed"
8890
/>
8991

0 commit comments

Comments
 (0)