Skip to content

Commit 5a7a1b8

Browse files
committed
fix(hmr): deep clone reused hoisted trees during dev
fix vitejs/vite#2022
1 parent c69f4ea commit 5a7a1b8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/runtime-core/src/vnode.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export function cloneVNode<T, U>(
459459
): VNode<T, U> {
460460
// This is intentionally NOT using spread or extend to avoid the runtime
461461
// key enumeration cost.
462-
const { props, ref, patchFlag } = vnode
462+
const { props, ref, patchFlag, children } = vnode
463463
const mergedProps = extraProps ? mergeProps(props || {}, extraProps) : props
464464
return {
465465
__v_isVNode: true,
@@ -479,7 +479,10 @@ export function cloneVNode<T, U>(
479479
: normalizeRef(extraProps)
480480
: ref,
481481
scopeId: vnode.scopeId,
482-
children: vnode.children,
482+
children:
483+
__DEV__ && patchFlag === PatchFlags.HOISTED && isArray(children)
484+
? (children as VNode[]).map(deepCloneVNode)
485+
: children,
483486
target: vnode.target,
484487
targetAnchor: vnode.targetAnchor,
485488
staticCount: vnode.staticCount,
@@ -513,6 +516,18 @@ export function cloneVNode<T, U>(
513516
}
514517
}
515518

519+
/**
520+
* Dev only, for HMR of hoisted vnodes reused in v-for
521+
* https://github.com/vitejs/vite/issues/2022
522+
*/
523+
function deepCloneVNode(vnode: VNode): VNode {
524+
const cloned = cloneVNode(vnode)
525+
if (isArray(vnode.children)) {
526+
cloned.children = (vnode.children as VNode[]).map(deepCloneVNode)
527+
}
528+
return cloned
529+
}
530+
516531
/**
517532
* @private
518533
*/

0 commit comments

Comments
 (0)