All files / server-renderer/src/helpers ssrRenderTeleport.ts

100% Statements 31/31
100% Branches 5/5
100% Functions 1/1
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 442x           2x   2x 14x 14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 14x 14x     14x   14x   14x 3x 3x 14x 11x 11x 11x 11x 11x 11x   14x 14x 14x  
import { type ComponentInternalInstance, ssrContextKey } from 'vue'
import {
  type PushFn,
  type SSRBufferItem,
  type SSRContext,
  createBuffer,
} from '../render'
 
export function ssrRenderTeleport(
  parentPush: PushFn,
  contentRenderFn: (push: PushFn) => void,
  target: string,
  disabled: boolean,
  parentComponent: ComponentInternalInstance,
): void {
  parentPush('<!--teleport start-->')
 
  const context = parentComponent.appContext.provides[
    ssrContextKey as any
  ] as SSRContext
  const teleportBuffers =
    context.__teleportBuffers || (context.__teleportBuffers = {})
  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 start anchor--><!--teleport anchor-->`
  } else {
    const { getBuffer, push } = createBuffer()
    push(`<!--teleport start anchor-->`)
    contentRenderFn(push)
    push(`<!--teleport anchor-->`)
    teleportContent = getBuffer()
  }
 
  targetBuffer.splice(bufferIndex, 0, teleportContent)
  parentPush('<!--teleport end-->')
}