Skip to content

Commit

Permalink
fix(hmr): ensure static nodes inherit DOM element in hmr mode
Browse files Browse the repository at this point in the history
fix #1156
  • Loading branch information
yyx990803 committed May 11, 2020
1 parent a165d82 commit 66c5a55
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
isSameVNodeType,
Static,
VNodeNormalizedRef,
VNodeHook
VNodeHook,
isVNode
} from './vnode'
import {
ComponentInternalInstance,
Expand All @@ -33,7 +34,8 @@ import {
ShapeFlags,
NOOP,
hasOwn,
invokeArrayFns
invokeArrayFns,
isArray
} from '@vue/shared'
import {
queueJob,
Expand Down Expand Up @@ -769,6 +771,9 @@ function baseCreateRenderer(
parentSuspense,
areChildrenSVG
)
if (__DEV__ && parentComponent && parentComponent.type.__hmrId) {
traverseStaticChildren(n1, n2)
}
} else if (!optimized) {
// full diff
patchChildren(
Expand Down Expand Up @@ -933,6 +938,9 @@ function baseCreateRenderer(
parentSuspense,
isSVG
)
if (__DEV__ && parentComponent && parentComponent.type.__hmrId) {
traverseStaticChildren(n1, n2)
}
} else {
// keyed / unkeyed, or manual fragments.
// for keyed & unkeyed, since they are compiler generated from v-for,
Expand Down Expand Up @@ -1944,6 +1952,31 @@ function baseCreateRenderer(
}
}

/**
* #1156
* When a component is HMR-enabled, we need to make sure that all static nodes
* inside a block also inherit the DOM element from the previous tree so that
* HMR updates (which are full updates) can retrieve the element for patching.
*
* Dev only.
*/
const traverseStaticChildren = (n1: VNode, n2: VNode) => {
const ch1 = n1.children
const ch2 = n2.children
if (isArray(ch1) && isArray(ch2)) {
for (let i = 0; i < ch1.length; i++) {
const c1 = ch1[i]
const c2 = ch2[i]
if (isVNode(c1) && isVNode(c2) && !c2.dynamicChildren) {
if (c2.patchFlag <= 0) {
c2.el = c1.el
}
traverseStaticChildren(c1, c2)
}
}
}
}

const render: RootRenderFunction = (vnode, container) => {
if (vnode == null) {
if (container._vnode) {
Expand Down

0 comments on commit 66c5a55

Please sign in to comment.