Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
63 changes: 63 additions & 0 deletions packages/runtime-core/__tests__/components/Suspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2395,4 +2395,67 @@ describe('Suspense', () => {
expect(unmounted).toHaveBeenCalledTimes(1)
})
})

// #14173
test('renders multiple async components in Suspense with v-for and updates list order with HOC', async () => {
const CompAsyncSetup = defineAsyncComponent({
props: ['item'],
render(ctx: any) {
return h('div', ctx.item.name)
},
})

const CompWrapper = {
props: ['item'],
render(ctx: any) {
return h(CompAsyncSetup, { item: ctx.item })
},
}

const items = ref([
{ id: 1, name: '111' },
{ id: 2, name: '222' },
{ id: 3, name: '333' },
])

const Comp = {
setup() {
return () =>
h(Suspense, null, {
default: () =>
h('div', [
h(
Fragment,
null,
items.value.map(item =>
h(CompWrapper, { item, key: item.id }),
),
),
]),
})
},
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

await nextTick()
await Promise.all(deps)

expect(serializeInner(root)).toBe(
`<div><div>111</div><div>222</div><div>333</div></div>`,
)

items.value = [
{ id: 4, name: '444' },
{ id: 5, name: '555' },
{ id: 6, name: '666' },
]

await nextTick()
await Promise.all(deps)
expect(serializeInner(root)).toBe(
`<div><div>444</div><div>555</div><div>666</div></div>`,
)
})
})
3 changes: 3 additions & 0 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ export function updateHOCHostEl(
}
if (root === vnode) {
;(vnode = parent.vnode).el = el
if (root.placeholder) {
vnode.placeholder = root.placeholder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should cause a memory leak, leading to the placeholder referencing a detached DOM node. Because when Suspense resolves, we only clear the placeholder of the async component. see

vnode.placeholder = null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct approach should be to attempt to locate the placeholder on anchorVNode.component.subTree here

anchorVNode.el || anchorVNode.placeholder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your guidance. I’ve made the changes accordingly.

}
parent = parent.parent
} else {
break
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ function baseCreateRenderer(
const placeholder = (instance.subTree = createVNode(Comment))
processCommentNode(null, placeholder, container!, anchor)
initialVNode.placeholder = placeholder.el
updateHOCHostEl(instance, placeholder.el)
}
} else {
setupRenderEffect(
Expand Down
Loading