Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(document-driven): add caching layer on client-side #1312

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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