Skip to content

Commit 06ab94e

Browse files
committed
feat(cache): add local caching on page/surround queries in dd mode
1 parent 477acef commit 06ab94e

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

src/runtime/composables/content.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
import { withoutTrailingSlash } from 'ufo'
12
import type { NavItem, ParsedContent } from '../types'
2-
import { computed, useState } from '#imports'
3+
import { computed, useState, useRoute } from '#imports'
34

45
export const useContentState = () => {
56
/**
6-
* Current page complete data.
7+
* Map of loaded pages.
78
*/
8-
const page = useState<ParsedContent>('dd-page')
9+
const pages = useState<Record<string, ParsedContent>>('dd-pages', () => ({}))
910

1011
/**
11-
* Navigation tree from root of app.
12+
* Previous and next page data.
13+
* Format: [prev, next]
1214
*/
13-
const navigation = useState<NavItem[]>('dd-navigation')
15+
const surrounds = useState<Record<string, Omit<ParsedContent, 'body'>>>('dd-surrounds', () => ({}))
1416

1517
/**
16-
* Previous and next page data.
17-
* Format: [prev, next]
18+
* Navigation tree from root of app.
1819
*/
19-
const surround = useState<Omit<ParsedContent, 'body'>[]>('dd-surround')
20+
const navigation = useState<NavItem[]>('dd-navigation')
2021

2122
/**
2223
* Globally loaded content files.
@@ -25,15 +26,35 @@ export const useContentState = () => {
2526
const globals = useState<Record<string, ParsedContent>>('dd-globals', () => ({}))
2627

2728
return {
28-
page,
29+
pages,
30+
surrounds,
2931
navigation,
30-
surround,
3132
globals
3233
}
3334
}
3435

3536
export const useContent = () => {
36-
const { navigation, page, surround, globals } = useContentState()
37+
const { navigation, pages, surrounds, globals } = useContentState()
38+
39+
const _path = computed(() => withoutTrailingSlash(useRoute().path))
40+
41+
/**
42+
* Current `page` key, computed from path and content state.
43+
*/
44+
const page = computed(
45+
() => {
46+
return pages.value[_path.value]
47+
}
48+
)
49+
50+
/**
51+
* Current `surround` key, computed from path and content state.
52+
*/
53+
const surround = computed(
54+
() => {
55+
return surrounds.value[_path.value]
56+
}
57+
)
3758

3859
/**
3960
* Table of contents from `page`.

src/runtime/pages/document-driven.vue

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@
22
import { useContent, useContentHead } from '#imports'
33
44
const { page } = useContent()
5-
const currentPage = ref(page.value)
6-
7-
watch(page, (v) => {
8-
if (v._id === currentPage.value._id) {
9-
currentPage.value = v
10-
}
11-
})
125
136
useContentHead(page)
147
</script>
158

169
<template>
1710
<div class="document-driven-page">
18-
<NuxtLayout v-if="currentPage" :name="currentPage.layout || 'default'">
19-
<ContentRenderer :key="currentPage._id" :value="currentPage">
11+
<NuxtLayout v-if="page" :name="page.layout || 'default'">
12+
<ContentRenderer :key="page._id" :value="page">
2013
<template #empty="{ value }">
2114
<DocumentDrivenEmpty :value="value" />
2215
</template>

src/runtime/plugins/documentDriven.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import layouts from '#build/layouts'
1010

1111
export default defineNuxtPlugin((nuxt) => {
1212
const { documentDriven: moduleOptions } = useRuntimeConfig()?.public?.content
13-
const pagesCache = new Map<string, ParsedContent>()
14-
const surroundCache = new Map<string, ParsedContent>()
1513

1614
/**
1715
* Finds a layout value from a cascade of objects.
@@ -52,11 +50,12 @@ export default defineNuxtPlugin((nuxt) => {
5250
return
5351
}
5452

55-
const { navigation, page, globals, surround } = useContentState()
53+
const { navigation, pages, globals, surrounds } = useContentState()
5654

5755
// Normalize route path
5856
const _path = withoutTrailingSlash(to.path)
5957

58+
// Promises array to be executed all at once
6059
const promises: (() => Promise<any> | any)[] = []
6160

6261
/**
@@ -136,14 +135,11 @@ export default defineNuxtPlugin((nuxt) => {
136135
*/
137136
if (moduleOptions.page && routeConfig.page !== false) {
138137
const pageQuery = () => {
139-
const { page } = useContentState()
138+
const { pages } = useContentState()
140139

141140
// Return same page as page is already loaded
142-
if (!force && page.value && page.value._path === _path) {
143-
return page.value
144-
}
145-
if (!force && process.client && pagesCache.has(_path)) {
146-
return pagesCache.get(_path)
141+
if (!force && pages.value[_path] && pages.value[_path]._path === _path) {
142+
return pages.value[_path]
147143
}
148144

149145
return queryContent()
@@ -163,12 +159,11 @@ export default defineNuxtPlugin((nuxt) => {
163159
*/
164160
if (moduleOptions.surround && routeConfig.surround !== false) {
165161
const surroundQuery = () => {
162+
const { surrounds } = useContentState()
163+
166164
// Return same surround as page is already loaded
167-
if (!force && page.value && page.value._path === _path) {
168-
return surround.value
169-
}
170-
if (!force && process.client && surroundCache.has(_path)) {
171-
return surroundCache.get(_path)
165+
if (!force && surrounds.value[_path]) {
166+
return surrounds.value[_path]
172167
}
173168

174169
return queryContent()
@@ -219,13 +214,11 @@ export default defineNuxtPlugin((nuxt) => {
219214
to.meta.layout = layoutName
220215

221216
// Update values
222-
page.value = _page
223-
process.client && pagesCache.set(_path, _page)
217+
pages.value[_path] = _page
224218
}
225219

226220
if (_surround) {
227-
surround.value = _surround
228-
process.client && surroundCache.set(_path, _surround)
221+
surrounds.value[_path] = _surround
229222
}
230223
})
231224
}

0 commit comments

Comments
 (0)