From ebf3e8470c72ae1d6c9f01faf4d58c1a1402848d Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Thu, 7 Jul 2022 16:48:47 +0200 Subject: [PATCH] fix(document-driven): rendering flash (#1336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Roe Co-authored-by: Sébastien Chopin Co-authored-by: Yaël Guilloux --- playground/document-driven/app.vue | 6 +++ .../components/content/Alert.vue | 17 ++++++++ .../components/content/List.vue | 14 ++++++ .../document-driven/content/10.alert.md | 10 +++++ playground/document-driven/content/9.list.md | 8 ++++ .../document-driven/layouts/reversed.vue | 5 +-- playground/document-driven/pages/vue-file.vue | 8 +++- playground/shared/layouts/blog.vue | 18 ++++++++ playground/shared/layouts/default.vue | 5 +-- src/module.ts | 28 +++++++++--- src/runtime/app.vue | 3 ++ src/runtime/composables/content.ts | 43 ++++++++++++++----- src/runtime/pages/document-driven.vue | 12 +++--- src/runtime/plugins/documentDriven.ts | 29 +++++-------- 14 files changed, 156 insertions(+), 50 deletions(-) create mode 100644 playground/document-driven/app.vue create mode 100644 playground/document-driven/components/content/Alert.vue create mode 100644 playground/document-driven/components/content/List.vue create mode 100644 playground/document-driven/content/10.alert.md create mode 100644 playground/document-driven/content/9.list.md create mode 100644 playground/shared/layouts/blog.vue create mode 100644 src/runtime/app.vue diff --git a/playground/document-driven/app.vue b/playground/document-driven/app.vue new file mode 100644 index 000000000..7b5d3519f --- /dev/null +++ b/playground/document-driven/app.vue @@ -0,0 +1,6 @@ + diff --git a/playground/document-driven/components/content/Alert.vue b/playground/document-driven/components/content/Alert.vue new file mode 100644 index 000000000..cbf53b133 --- /dev/null +++ b/playground/document-driven/components/content/Alert.vue @@ -0,0 +1,17 @@ + diff --git a/playground/document-driven/components/content/List.vue b/playground/document-driven/components/content/List.vue new file mode 100644 index 000000000..8142fde89 --- /dev/null +++ b/playground/document-driven/components/content/List.vue @@ -0,0 +1,14 @@ + + + diff --git a/playground/document-driven/content/10.alert.md b/playground/document-driven/content/10.alert.md new file mode 100644 index 000000000..0368020c9 --- /dev/null +++ b/playground/document-driven/content/10.alert.md @@ -0,0 +1,10 @@ +--- +layout: blog +--- + +::alert{icon="ph:circle-wavy-warning-duotone"} +#title +This is an alert +#default +This is the default content of my alert! +:: diff --git a/playground/document-driven/content/9.list.md b/playground/document-driven/content/9.list.md new file mode 100644 index 000000000..b7b5a5974 --- /dev/null +++ b/playground/document-driven/content/9.list.md @@ -0,0 +1,8 @@ +::list +- Item 1 + ::list{icon="ph:check-circle-light"} + - Item 1.1 + - Item 1.2 + :: +- Item 2 +:: diff --git a/playground/document-driven/layouts/reversed.vue b/playground/document-driven/layouts/reversed.vue index 5a8dfd8c6..96d55c497 100644 --- a/playground/document-driven/layouts/reversed.vue +++ b/playground/document-driven/layouts/reversed.vue @@ -1,9 +1,6 @@ diff --git a/playground/document-driven/pages/vue-file.vue b/playground/document-driven/pages/vue-file.vue index 67d9619b2..dd8c14b3b 100644 --- a/playground/document-driven/pages/vue-file.vue +++ b/playground/document-driven/pages/vue-file.vue @@ -1,6 +1,8 @@ diff --git a/playground/shared/layouts/blog.vue b/playground/shared/layouts/blog.vue new file mode 100644 index 000000000..2c834a893 --- /dev/null +++ b/playground/shared/layouts/blog.vue @@ -0,0 +1,18 @@ + + + diff --git a/playground/shared/layouts/default.vue b/playground/shared/layouts/default.vue index 454a8f9b8..a14b00bcb 100644 --- a/playground/shared/layouts/default.vue +++ b/playground/shared/layouts/default.vue @@ -1,10 +1,7 @@ diff --git a/src/module.ts b/src/module.ts index 75867ccf7..23046747d 100644 --- a/src/module.ts +++ b/src/module.ts @@ -448,12 +448,28 @@ export default defineNuxtModule({ nuxt.options.pages = true nuxt.hook('pages:extend', (pages) => { - pages.unshift({ - name: 'slug', - path: '/:slug(.*)*', - file: resolveRuntimeModule('./pages/document-driven.vue'), - children: [] - }) + // Respect user's custom catch-all page + if (!pages.find(page => page.path === '/:slug(.*)*')) { + pages.unshift({ + name: 'slug', + path: '/:slug(.*)*', + file: resolveRuntimeModule('./pages/document-driven.vue'), + children: [] + }) + } + }) + nuxt.hook('app:resolve', async (app) => { + if (app.mainComponent?.includes('@nuxt/ui-templates')) { + app.mainComponent = resolveRuntimeModule('./app.vue') + } else { + const appContent = await fs.promises.readFile(app.mainComponent!, { encoding: 'utf-8' }) + if (appContent.includes('` inside `app.vue` will cause unwanted layout shifting in your application.', + 'Consider removing `` from `app.vue` and using it in your pages.' + ].join('')) + } + } }) } } else { diff --git a/src/runtime/app.vue b/src/runtime/app.vue new file mode 100644 index 000000000..8f62b8bf9 --- /dev/null +++ b/src/runtime/app.vue @@ -0,0 +1,3 @@ + diff --git a/src/runtime/composables/content.ts b/src/runtime/composables/content.ts index f28d4a2c5..d49a5f730 100644 --- a/src/runtime/composables/content.ts +++ b/src/runtime/composables/content.ts @@ -1,22 +1,23 @@ +import { withoutTrailingSlash } from 'ufo' import type { NavItem, ParsedContent } from '../types' -import { computed, useState } from '#imports' +import { computed, useState, useRoute } from '#imports' export const useContentState = () => { /** - * Current page complete data. + * Map of loaded pages. */ - const page = useState('dd-page') + const pages = useState>('dd-pages', () => ({})) /** - * Navigation tree from root of app. + * Previous and next page data. + * Format: [prev, next] */ - const navigation = useState('dd-navigation') + const surrounds = useState>>('dd-surrounds', () => ({})) /** - * Previous and next page data. - * Format: [prev, next] + * Navigation tree from root of app. */ - const surround = useState[]>('dd-surround') + const navigation = useState('dd-navigation') /** * Globally loaded content files. @@ -25,15 +26,35 @@ export const useContentState = () => { const globals = useState>('dd-globals', () => ({})) return { - page, + pages, + surrounds, navigation, - surround, globals } } export const useContent = () => { - const { navigation, page, surround, globals } = useContentState() + const { navigation, pages, surrounds, globals } = useContentState() + + const _path = computed(() => withoutTrailingSlash(useRoute().path)) + + /** + * Current `page` key, computed from path and content state. + */ + const page = computed( + () => { + return pages.value[_path.value] + } + ) + + /** + * Current `surround` key, computed from path and content state. + */ + const surround = computed( + () => { + return surrounds.value[_path.value] + } + ) /** * Table of contents from `page`. diff --git a/src/runtime/pages/document-driven.vue b/src/runtime/pages/document-driven.vue index 5285172c8..3033ec59e 100644 --- a/src/runtime/pages/document-driven.vue +++ b/src/runtime/pages/document-driven.vue @@ -8,11 +8,13 @@ useContentHead(page) diff --git a/src/runtime/plugins/documentDriven.ts b/src/runtime/plugins/documentDriven.ts index 5c5178842..46349d5bd 100644 --- a/src/runtime/plugins/documentDriven.ts +++ b/src/runtime/plugins/documentDriven.ts @@ -10,8 +10,6 @@ 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. @@ -52,11 +50,12 @@ export default defineNuxtPlugin((nuxt) => { return } - const { navigation, page, globals, surround } = useContentState() + const { navigation, pages, globals, surrounds } = useContentState() // Normalize route path const _path = withoutTrailingSlash(to.path) + // Promises array to be executed all at once const promises: (() => Promise | any)[] = [] /** @@ -136,14 +135,11 @@ export default defineNuxtPlugin((nuxt) => { */ if (moduleOptions.page && routeConfig.page !== false) { const pageQuery = () => { - const { page } = useContentState() + const { pages } = useContentState() // Return same page as page is already loaded - if (!force && page.value && page.value._path === _path) { - return page.value - } - if (!force && process.client && pagesCache.has(_path)) { - return pagesCache.get(_path) + if (!force && pages.value[_path] && pages.value[_path]._path === _path) { + return pages.value[_path] } return queryContent() @@ -163,12 +159,11 @@ export default defineNuxtPlugin((nuxt) => { */ if (moduleOptions.surround && routeConfig.surround !== false) { const surroundQuery = () => { + const { surrounds } = useContentState() + // Return same surround as page is already loaded - if (!force && page.value && page.value._path === _path) { - return surround.value - } - if (!force && process.client && surroundCache.has(_path)) { - return surroundCache.get(_path) + if (!force && surrounds.value[_path]) { + return surrounds.value[_path] } return queryContent() @@ -219,13 +214,11 @@ export default defineNuxtPlugin((nuxt) => { to.meta.layout = layoutName // Update values - page.value = _page - process.client && pagesCache.set(_path, _page) + pages.value[_path] = _page } if (_surround) { - surround.value = _surround - process.client && surroundCache.set(_path, _surround) + surrounds.value[_path] = _surround } }) }