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

perf: keep document-drive state in shallowRef and prefetch pages/components #2118

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/runtime/composables/content.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { withoutTrailingSlash } from 'ufo'
import type { NavItem, ParsedContent } from '../types'
import { computed, useState, useRoute } from '#imports'
import { computed, shallowReactive, shallowRef, useState, useRoute } from '#imports'

export const useContentState = () => {
/**
* Map of loaded pages.
*/
const pages = useState<Record<string, ParsedContent>>('dd-pages', () => ({}))
const pages = useState<Record<string, ParsedContent>>('dd-pages', () => shallowRef(shallowReactive({})))

/**
* Previous and next page data.
* Format: [prev, next]
*/
const surrounds = useState<Record<string, Omit<ParsedContent, 'body'>>>('dd-surrounds', () => ({}))
const surrounds = useState<Record<string, Omit<ParsedContent, 'body'>>>('dd-surrounds', () => shallowRef(shallowReactive({})))

/**
* Navigation tree from root of app.
Expand All @@ -23,7 +23,7 @@ export const useContentState = () => {
* Globally loaded content files.
* Format: { [key: string]: ParsedContent }
*/
const globals = useState<Record<string, ParsedContent>>('dd-globals', () => ({}))
const globals = useState<Record<string, ParsedContent>>('dd-globals', () => shallowRef(shallowReactive({})))

return {
pages,
Expand Down
41 changes: 39 additions & 2 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router'
// @ts-ignore
import { useRuntimeConfig, addRouteMiddleware, callWithNuxt, navigateTo, useRoute, defineNuxtPlugin } from '#app'
import { useRuntimeConfig, addRouteMiddleware, callWithNuxt, navigateTo, useRoute, defineNuxtPlugin, prefetchComponents, useRouter } from '#app'
import { withoutTrailingSlash, hasProtocol } from 'ufo'
import type { NavItem, ParsedContent } from '../types'
import { pascalCase } from 'scule'
import type { MarkdownNode, NavItem, ParsedContent } from '../types'
import type { ModuleOptions } from '../../module'
import { useContentState } from '../composables/content'
import { useContentHelpers } from '../composables/helpers'
import { fetchContentNavigation } from '../composables/navigation'
import { queryContent } from '../composables/query'
import { componentNames } from '#components'
// @ts-ignore
import layouts from '#build/layouts'

Expand Down Expand Up @@ -251,6 +253,25 @@ export default defineNuxtPlugin((nuxt) => {
})
}

// Prefetch page content
if (process.client) {
const router = useRouter()
nuxt.hook('link:prefetch', (link) => {
if (!(link in pages.value) && !hasProtocol(link)) {
const route = router.resolve(link)
if (route.matched.length > 0) {
refresh(route)
}
}
})
// @ts-expect-error hook is not typed
nuxt.hooks.hook('content:document-driven:finish', ({ page }) => {
if (page?.body?.children) {
prefetchBodyComponents(page.body.children)
}
})
}

// Route middleware
addRouteMiddleware(async (to, from) => {
// TODO: Remove this (https://github.com/nuxt/framework/pull/5274)
Expand Down Expand Up @@ -284,3 +305,19 @@ export default defineNuxtPlugin((nuxt) => {
// @ts-ignore - Refresh on client-side
nuxt.hook('app:data:refresh', async () => process.client && await refresh(useRoute(), true))
})

function prefetchBodyComponents (nodes: MarkdownNode[]) {
for (const node of nodes) {
if (node.children) {
prefetchBodyComponents(node.children)
}
if (node.type === 'element' && node.tag) {
const el = pascalCase(node.tag)
for (const name of ['Prose' + el, el]) {
if (componentNames.includes(name)) {
prefetchComponents(name)
}
}
}
}
}