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

fix(ContentRendererMarkdown): preload components used in content #1309

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion src/runtime/components/ContentRendererMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ export default defineComponent({
default: 'div'
}
},
setup () {
async setup (props) {
const { content: { tags = {} } } = useRuntimeConfig().public

await resolveContentComponents(props.value.body, {
tags: {
...tags,
...props.value?.tags || {}
}
})

return { tags }
},
render (ctx) {
Expand Down Expand Up @@ -363,3 +370,28 @@ function mergeTextNodes (nodes: Array<VNode>) {
}
return mergedNodes
}

async function resolveContentComponents (body, meta) {
const components = Array.from(new Set(loadComponents(body, meta)))
await Promise.all(components.map(async (c) => {
const resolvedComponent = resolveComponent(c) as any
if (resolvedComponent?.__asyncLoader && !resolvedComponent.__asyncResolved) {
await resolvedComponent.__asyncLoader()
}
}))

function loadComponents (node, documentMeta) {
if (node.type === 'text' || node.tag === 'binding') {
return []
}
const renderTag: string = (typeof node.props?.__ignoreMap === 'undefined' && documentMeta.tags[node.tag!]) || node.tag!
const components: string[] = []
if (node.type !== 'root' && !htmlTags.includes(renderTag as any)) {
components.push(renderTag)
}
for (const child of (node.children || [])) {
components.push(...loadComponents(child, documentMeta))
}
return components
}
}
30 changes: 15 additions & 15 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ export default defineNuxtPlugin((nuxt) => {
_page,
_surround
]) => {
// Find used layout
const layoutName = findLayout(to, _page, _navigation, _globals)

// Prefetch layout component
const layout = layouts[layoutName]

if (layout && layout?.__asyncLoader && !layout.__asyncResolved) {
await layout.__asyncLoader()
}
// Apply layout
to.meta.layout = layoutName

if (_navigation) {
navigation.value = _navigation
}
Expand All @@ -192,25 +204,13 @@ export default defineNuxtPlugin((nuxt) => {
surround.value = _surround
}

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

if (_page) {
// Update values
page.value = _page
}

// Find used layout
const layoutName = findLayout(to, _page, _navigation, _globals)

// Prefetch layout component
const layout = layouts[layoutName]
if (layout && layout?.__asyncLoader && !layout.__asyncResolved) {
await layout.__asyncLoader()
}

// Apply layout
to.meta.layout = layoutName
})
}

Expand Down