Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/runtime-vapor/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,39 @@ test('should stop effects of the previous children render when the slot re-runs'
expect(effectRuns).toBe(1)
})

test('should not call update hooks when mounting teleport inside an element', async () => {
const target = document.createElement('div')
const state = reactive({ msg: 'one' })
const beforeUpdate = vi.fn()
const updated = vi.fn()
const App = compile(
`<script setup vapor>
import { onBeforeUpdate, onUpdated } from 'vue'
const data = _data
onBeforeUpdate(data.beforeUpdate)
onUpdated(data.updated)
</script>
<template>
<div>
<Teleport :to="data.target"><p>{{ data.state.msg }}</p></Teleport>
</div>
</template>`,
{ target, state, beforeUpdate, updated } as any,
)

define(App).render()
await nextTick()
expect(target.innerHTML).toBe('<p>one</p>')
expect(beforeUpdate).toHaveBeenCalledTimes(0)
expect(updated).toHaveBeenCalledTimes(0)

state.msg = 'two'
await nextTick()
expect(target.innerHTML).toBe('<p>two</p>')
expect(beforeUpdate).toHaveBeenCalledTimes(1)
expect(updated).toHaveBeenCalledTimes(1)
})

test('should cache delayed teleported child under KeepAlive once target becomes available', async () => {
const show = ref(true)
const target = ref<any>('#missing-teleport-target-keepalive')
Expand Down
11 changes: 7 additions & 4 deletions packages/runtime-vapor/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ export class TeleportFragment extends RenderContextFragment {
}

private initChildren(): void {
const prevInstance = setCurrentInstance(
this.renderInstance,
this.childrenScope,
)
const instance = this.renderInstance as VaporComponentInstance | null
const prevInstance = setCurrentInstance(instance, this.childrenScope)
// Deferred init can run after the owner mounted, but the first render of
// the children is still part of the mount and must not run update hooks.
const prevUpdating = instance ? instance.isUpdating : false
if (instance) instance.isUpdating = true
try {
this.childrenInitialized = true
// RenderEffect restores (renderInstance, childrenScope) on every run,
Expand All @@ -176,6 +178,7 @@ export class TeleportFragment extends RenderContextFragment {
if (owner && owner.applyCssVars) registerCssVarOutlet(owner, this)
this.bindChildren(this.nodes)
} finally {
if (instance) instance.isUpdating = prevUpdating
restoreCurrentInstance(prevInstance)
}
}
Expand Down
Loading