Skip to content
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

fix(runtime-core): the component VNode should be cloned when reusing it #3670

Merged
merged 4 commits into from
May 27, 2021
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
35 changes: 35 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,39 @@ describe('renderer: component', () => {
expect(serializeInner(root)).toBe(`<h1>1</h1>`)
})
})

test('the component VNode should be cloned when reusing it', () => {
const Child = {
setup(props: any, { slots }: SetupContext) {
return () => {
const c = slots.default!()
return [c, c, c]
}
}
}

const ids: number[] = []
const Comp = {
render: () => h('h1'),
beforeUnmount() {
ids.push((this as any).$.uid)
}
}

const App = {
setup() {
return () => {
return h(Child, () => [h(Comp)])
}
}
}

const root = nodeOps.createElement('div')
render(h(App), root)
expect(serializeInner(root)).toBe(`<h1></h1><h1></h1><h1></h1>`)

render(null, root)
expect(serializeInner(root)).toBe(``)
expect(ids).toEqual([ids[0], ids[0] + 1, ids[0] + 2])
})
})
9 changes: 7 additions & 2 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,16 @@ export function normalizeVNode(child: VNodeChild): VNode {
return createVNode(Comment)
} else if (isArray(child)) {
// fragment
return createVNode(Fragment, null, child)
return createVNode(
Fragment,
null,
// #3666, avoid reference pollution when reusing vnode
child.slice()
)
} else if (typeof child === 'object') {
// already vnode, this should be the most common since compiled templates
// always produce all-vnode children arrays
return child.el === null ? child : cloneVNode(child)
return cloneIfMounted(child)
} else {
// strings and numbers
return createVNode(Text, null, String(child))
Expand Down