Skip to content

Commit 82f0e46

Browse files
committed
wip: save
1 parent d17b8dd commit 82f0e46

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

packages/runtime-vapor/__tests__/hydration.spec.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3325,7 +3325,77 @@ describe('Vapor Mode hydration', () => {
33253325
expect(teleportContainer.innerHTML).toBe('')
33263326
})
33273327

3328-
test('unmount (mismatch + full integration)', async () => {})
3328+
// TODO: need merge mismatch handling code from #13226
3329+
test.todo('unmount (mismatch + full integration)', async () => {
3330+
const targetId = 'teleport7'
3331+
const data = ref({
3332+
toggle: ref(true),
3333+
})
3334+
3335+
const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
3336+
const Comp1 = compileVaporComponent(template1)
3337+
const SSRComp1 = compileVaporComponent(
3338+
template1,
3339+
undefined,
3340+
undefined,
3341+
true,
3342+
)
3343+
3344+
const template2 = `<div>Comp2</div>`
3345+
const Comp2 = compileVaporComponent(template2)
3346+
const SSRComp2 = compileVaporComponent(
3347+
template2,
3348+
undefined,
3349+
undefined,
3350+
true,
3351+
)
3352+
3353+
const appCode = `
3354+
<div>
3355+
<components.Comp1 v-if="data.toggle"/>
3356+
<components.Comp2 v-else/>
3357+
</div>
3358+
`
3359+
3360+
const SSRApp = compileVaporComponent(
3361+
appCode,
3362+
data,
3363+
{
3364+
Comp1: SSRComp1,
3365+
Comp2: SSRComp2,
3366+
},
3367+
true,
3368+
)
3369+
3370+
const teleportContainer = document.createElement('div')
3371+
teleportContainer.id = targetId
3372+
document.body.appendChild(teleportContainer)
3373+
3374+
const mainHtml = await VueServerRenderer.renderToString(
3375+
runtimeDom.createSSRApp(SSRApp),
3376+
)
3377+
expect(mainHtml).toBe(
3378+
'<div><!--teleport start--><!--teleport end--></div>',
3379+
)
3380+
// teleportContainer.innerHTML =
3381+
expect(teleportContainer.innerHTML).toBe('')
3382+
3383+
const { container } = await mountWithHydration(mainHtml, appCode, data, {
3384+
Comp1,
3385+
Comp2,
3386+
})
3387+
3388+
expect(container.innerHTML).toBe(
3389+
'<div><!--teleport start--><!--teleport end--><!--if--></div>',
3390+
)
3391+
expect(teleportContainer.innerHTML).toBe(`<span>Teleported Comp1</span>`)
3392+
expect(`mismatch`).toHaveBeenWarned()
3393+
3394+
data.value.toggle = false
3395+
await nextTick()
3396+
expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
3397+
expect(teleportContainer.innerHTML).toBe('')
3398+
})
33293399

33303400
test('target change (mismatch + full integration)', async () => {})
33313401
})

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,12 @@ export class TeleportFragment extends VaporFragment {
234234
let nextNode = this.placeholder.nextSibling!
235235
setCurrentHydrationNode(nextNode)
236236
this.mountAnchor = this.anchor = locateTeleportEndAnchor(nextNode)!
237+
this.mountContainer = this.anchor.parentNode
237238
this.targetStart = targetNode
238239
this.targetAnchor = targetNode && targetNode.nextSibling
239-
this.mountContainer = this.anchor.parentNode
240240
} else {
241241
this.anchor = locateTeleportEndAnchor()!
242+
this.mountContainer = target
242243
let targetAnchor = targetNode
243244
while (targetAnchor) {
244245
if (targetAnchor && targetAnchor.nodeType === 8) {
@@ -253,8 +254,19 @@ export class TeleportFragment extends VaporFragment {
253254
}
254255
targetAnchor = targetAnchor.nextSibling
255256
}
257+
258+
// if the HTML corresponding to Teleport is not embedded in the
259+
// correct position on the final page during SSR. the targetAnchor will
260+
// always be null, we need to manually add targetAnchor to ensure
261+
// Teleport it can properly unmount or move
262+
if (!this.targetAnchor) {
263+
target.appendChild((this.targetStart = createTextNode('1')))
264+
target.appendChild(
265+
(this.mountAnchor = this.targetAnchor = createTextNode('2')),
266+
)
267+
}
268+
256269
if (targetNode) {
257-
this.mountContainer = this.targetAnchor!.parentNode
258270
setCurrentHydrationNode(targetNode.nextSibling)
259271
}
260272
}

0 commit comments

Comments
 (0)