Skip to content

fix(Teleport): handle deferred teleport update before mounted #12168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2024
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
43 changes: 43 additions & 0 deletions packages/runtime-core/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,49 @@ describe('renderer: teleport', () => {
`</div>`,
)
})

test('update before mounted with defer', async () => {
const root = document.createElement('div')
document.body.appendChild(root)

const show = ref(false)
const foo = ref('foo')
const Header = {
props: { foo: String },
setup(props: any) {
return () => h('div', props.foo)
},
}
const Footer = {
setup() {
foo.value = 'bar'
return () => h('div', 'Footer')
},
}
createDOMApp({
render() {
return show.value
? [
h(
Teleport,
{ to: '#targetId', defer: true },
h(Header, { foo: foo.value }),
),
h(Footer),
h('div', { id: 'targetId' }),
]
: [h('div')]
},
}).mount(root)

expect(root.innerHTML).toMatchInlineSnapshot(`"<div></div>"`)

show.value = true
await nextTick()
expect(root.innerHTML).toMatchInlineSnapshot(
`"<!--teleport start--><!--teleport end--><div>Footer</div><div id="targetId"><div>bar</div></div>"`,
)
})
})

function runSharedTests(deferMode: boolean) {
Expand Down
23 changes: 22 additions & 1 deletion packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,32 @@ export const TeleportImpl = {
}

if (isTeleportDeferred(n2.props)) {
queuePostRenderEffect(mountToTarget, parentSuspense)
queuePostRenderEffect(() => {
mountToTarget()
n2.el!.__isMounted = true
}, parentSuspense)
} else {
mountToTarget()
}
} else {
if (isTeleportDeferred(n2.props) && !n1.el!.__isMounted) {
queuePostRenderEffect(() => {
TeleportImpl.process(
n1,
n2,
container,
anchor,
parentComponent,
parentSuspense,
namespace,
slotScopeIds,
optimized,
internals,
)
delete n1.el!.__isMounted
}, parentSuspense)
return
}
// update content
n2.el = n1.el
n2.targetStart = n1.targetStart
Expand Down