Skip to content

Commit b005811

Browse files
authored
fix(runtime-vapor): force defer mount when teleport has insertion state (#14049)
1 parent bf2d2b2 commit b005811

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/runtime-vapor/__tests__/components/Teleport.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ describe('renderer: VaporTeleport', () => {
192192
expect(root.innerHTML).toBe(
193193
'<!--teleport start--><!--teleport end--><div>root 2</div>',
194194
)
195+
await nextTick()
195196
expect(target.innerHTML).toBe('<div>teleported 2</div>')
196197
})
197198

@@ -367,6 +368,7 @@ describe('renderer: VaporTeleport', () => {
367368
expect(root.innerHTML).toBe(
368369
'<!--teleport start--><!--teleport end--><div>root 2</div>',
369370
)
371+
await nextTick()
370372
expect(target.innerHTML).toBe('<div>teleported 2</div>')
371373

372374
// reload parent again by changing disabled
@@ -1255,4 +1257,41 @@ function runSharedTests(deferMode: boolean): void {
12551257
expect(child.outerHTML).toBe(`<div>teleported</div>`)
12561258
expect(tRefInMounted).toBe(child)
12571259
})
1260+
1261+
test('with insertion state', async () => {
1262+
const root = document.createElement('div')
1263+
document.body.appendChild(root)
1264+
1265+
const Comp = defineVaporComponent({
1266+
setup() {
1267+
return template('content')()
1268+
},
1269+
})
1270+
1271+
const { app, mount } = define({
1272+
setup() {
1273+
const n0 = template('<div id="tt"></div>')()
1274+
const n4 = template('<div></div>')() as any
1275+
setInsertionState(n4, null, true)
1276+
createComponent(
1277+
VaporTeleport,
1278+
{ to: () => '#tt' },
1279+
{
1280+
default: () =>
1281+
createComponent(Comp, null, {
1282+
default: () => template('content')(),
1283+
}),
1284+
},
1285+
)
1286+
return [n0, n4]
1287+
},
1288+
}).create()
1289+
1290+
mount(root)
1291+
1292+
await nextTick()
1293+
const target = document.querySelector('#tt')!
1294+
expect(target.innerHTML).toBe('content')
1295+
app.unmount()
1296+
})
12581297
}

packages/runtime-vapor/src/components/Teleport.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ export class TeleportFragment extends VaporFragment {
165165
}
166166
// mount into target container
167167
else {
168-
if (isTeleportDeferred(this.resolvedProps!)) {
168+
if (
169+
isTeleportDeferred(this.resolvedProps!) ||
170+
// force defer when the parent is not connected to the DOM,
171+
// typically due to an early insertion caused by setInsertionState.
172+
!this.parent!.isConnected
173+
) {
169174
queuePostFlushCb(mountToTarget)
170175
} else {
171176
mountToTarget()

0 commit comments

Comments
 (0)