From 019c7f807cf4ad0bf1f8d4680e9bb041e8a05197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 29 Jun 2022 17:57:00 +0200 Subject: [PATCH] feat(document-driven): add caching layer on client-side (#1312) --- src/runtime/plugins/documentDriven.ts | 33 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/runtime/plugins/documentDriven.ts b/src/runtime/plugins/documentDriven.ts index c5cc07f4f..6c524641c 100644 --- a/src/runtime/plugins/documentDriven.ts +++ b/src/runtime/plugins/documentDriven.ts @@ -10,6 +10,8 @@ import layouts from '#build/layouts' export default defineNuxtPlugin((nuxt) => { const { documentDriven: moduleOptions } = useRuntimeConfig()?.public?.content + const pagesCache = new Map() + const surroundCache = new Map() /** * Finds a layout value from a cascade of objects. @@ -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 }) @@ -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({ @@ -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) @@ -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) } }) }