| 8 | } from '../render' |
| 9 | |
| 10 | export function ssrRenderTeleport( |
| 11 | parentPush: PushFn, |
| 12 | contentRenderFn: (push: PushFn) => void, |
| 13 | target: string, |
| 14 | disabled: boolean, |
| 15 | parentComponent: ComponentInternalInstance, |
| 16 | ): void { |
| 17 | parentPush('<!--teleport start-->') |
| 18 | |
| 19 | const context = parentComponent.appContext.provides[ |
| 20 | ssrContextKey as any |
| 21 | ] as SSRContext |
| 22 | const teleportBuffers = |
| 23 | context.__teleportBuffers || (context.__teleportBuffers = {}) |
| 24 | const targetBuffer = teleportBuffers[target] || (teleportBuffers[target] = []) |
| 25 | // record current index of the target buffer to handle nested teleports |
| 26 | // since the parent needs to be rendered before the child |
| 27 | const bufferIndex = targetBuffer.length |
| 28 | |
| 29 | let teleportContent: SSRBufferItem |
| 30 | |
| 31 | if (disabled) { |
| 32 | contentRenderFn(parentPush) |
| 33 | teleportContent = `<!--teleport start anchor--><!--teleport anchor-->` |
| 34 | } else { |
| 35 | const { getBuffer, push } = createBuffer() |
| 36 | push(`<!--teleport start anchor-->`) |
| 37 | contentRenderFn(push) |
| 38 | push(`<!--teleport anchor-->`) |
| 39 | teleportContent = getBuffer() |
| 40 | } |
| 41 | |
| 42 | targetBuffer.splice(bufferIndex, 0, teleportContent) |
| 43 | if ( |
| 44 | isPromise(teleportContent) || |
| 45 | (isArray(teleportContent) && teleportContent.hasAsync) |
| 46 | ) { |
| 47 | targetBuffer.hasAsync = true |
| 48 | } |
| 49 | parentPush('<!--teleport end-->') |
| 50 | } |