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

100% Statements 17/17
100% Branches 6/6
100% Functions 1/1
100% Lines 17/17

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 3813x 13x   13x             11x   11x       11x 11x     11x       11x 3x 3x   8x 8x 8x 8x     11x 11x    
import { ComponentInternalInstance, ssrContextKey } from 'vue'
import { createBuffer, PushFn, SSRBufferItem, SSRContext } from '../render'
 
export function ssrRenderTeleport(
  parentPush: PushFn,
  contentRenderFn: (push: PushFn) => void,
  target: string,
  disabled: boolean,
  parentComponent: ComponentInternalInstance
) {
  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 anchor-->`
  } else {
    const { getBuffer, push } = createBuffer()
    contentRenderFn(push)
    push(`<!--teleport anchor-->`)
    teleportContent = getBuffer()
  }
 
  targetBuffer.splice(bufferIndex, 0, teleportContent)
  parentPush('<!--teleport end-->')
}