Skip to content

Commit

Permalink
feat(document-driven): add caching layer on client-side (#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Jun 29, 2022
1 parent fb1d9c3 commit d654826
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import layouts from '#build/layouts'

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

/**
* Finds a layout value from a cascade of objects.
Expand Down Expand Up @@ -134,6 +136,9 @@ export default defineNuxtPlugin((nuxt) => {
if (!force && page.value && page.value._path === _path) {
return page.value
}
if (!force && process.client && pagesCache.has(_path)) {
return pagesCache.get(_path)
}

return queryContent()
.where({ _path })
Expand All @@ -156,6 +161,9 @@ export default defineNuxtPlugin((nuxt) => {
if (!force && page.value && page.value._path === _path) {
return surround.value
}
if (!force && process.client && surroundCache.has(_path)) {
return surroundCache.get(_path)
}

return queryContent()
.where({
Expand All @@ -180,6 +188,13 @@ export default defineNuxtPlugin((nuxt) => {
_page,
_surround
]) => {
if (_navigation) {
navigation.value = _navigation
}

if (_globals) {
globals.value = _globals
}
// Find used layout
const layoutName = findLayout(to, _page, _navigation, _globals)

Expand All @@ -192,24 +207,18 @@ export default defineNuxtPlugin((nuxt) => {
// Apply layout
to.meta.layout = layoutName

if (_navigation) {
navigation.value = _navigation
}

if (_globals) {
globals.value = _globals
}

if (_surround) {
surround.value = _surround
}

// Use `redirect` key to redirect to another page
if (_page?.redirect) { return _page?.redirect }

if (_page) {
// Update values
page.value = _page
process.client && pagesCache.set(_path, _page)
}

if (_surround) {
surround.value = _surround
process.client && surroundCache.set(_path, _surround)
}
})
}
Expand Down

0 comments on commit d654826

Please sign in to comment.