fix(ssr/teleport): support nested teleports in ssr

fix #5242
This commit is contained in:
Evan You
2022-05-18 18:13:08 +08:00
parent 84f0353511
commit 595263c0e9
4 changed files with 88 additions and 31 deletions

View File

@@ -10,28 +10,28 @@ export function ssrRenderTeleport(
) {
parentPush('<!--teleport start-->')
let teleportContent: SSRBufferItem
if (disabled) {
contentRenderFn(parentPush)
teleportContent = `<!---->`
} else {
const { getBuffer, push } = createBuffer()
contentRenderFn(push)
push(`<!---->`) // teleport end anchor
teleportContent = getBuffer()
}
const context = parentComponent.appContext.provides[
ssrContextKey as any
] as SSRContext
const teleportBuffers =
context.__teleportBuffers || (context.__teleportBuffers = {})
if (teleportBuffers[target]) {
teleportBuffers[target].push(teleportContent)
const targetBuffer = teleportBuffers[target] || (teleportBuffers[target] = [])
// record current index of the target buffer to handle nested teleports
// since the parent needs to be rendered before the child
const bufferIndex = targetBuffer.length
let teleportContent: SSRBufferItem
if (disabled) {
contentRenderFn(parentPush)
teleportContent = `<!--teleport anchor-->`
} else {
teleportBuffers[target] = [teleportContent]
const { getBuffer, push } = createBuffer()
contentRenderFn(push)
push(`<!--teleport anchor-->`)
teleportContent = getBuffer()
}
targetBuffer.splice(bufferIndex, 0, teleportContent)
parentPush('<!--teleport end-->')
}