Skip to content

Commit 79bcaff

Browse files
committed
chore: improve code
1 parent db5a631 commit 79bcaff

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

packages/runtime-core/src/hydration.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,7 @@ export function createHydrationFunctions(
152152
.firstChild!
153153

154154
// replace <template> node with inner child
155-
replace(content, node)
156-
let parent = parentComponent
157-
while (parent) {
158-
if (parent.vnode.el === node) {
159-
parent.vnode.el = content
160-
parent.subTree.el = content
161-
}
162-
parent = parent.parent
163-
}
164-
node = content
155+
node = replaceNode(content, node, parentComponent)
165156
nextNode = nextSibling(node)
166157
} else {
167158
nextNode = onMismatch()
@@ -406,16 +397,7 @@ export function createHydrationFunctions(
406397
dirs && invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
407398

408399
// replace <template> node with inner child
409-
replace(content, el)
410-
let parent = parentComponent
411-
while (parent) {
412-
if (parent.vnode.el === el) {
413-
parent.vnode.el = content
414-
parent.subTree.el = content
415-
}
416-
parent = parent.parent
417-
}
418-
el = content
400+
el = replaceNode(content, el, parentComponent) as Element
419401
} else if (dirs) {
420402
invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
421403
}
@@ -640,5 +622,27 @@ export function createHydrationFunctions(
640622
return node
641623
}
642624

625+
const replaceNode = (
626+
newNode: Node,
627+
oldNode: Node,
628+
parentComponent: ComponentInternalInstance | null
629+
): Node => {
630+
// replace node
631+
replace(newNode, oldNode)
632+
633+
// update vnode
634+
let parent = parentComponent
635+
while (parent) {
636+
if (parent.vnode.el === oldNode) {
637+
parent.vnode.el = newNode
638+
parent.subTree.el = newNode
639+
}
640+
parent = parent.parent
641+
}
642+
643+
oldNode = newNode
644+
return oldNode
645+
}
646+
643647
return [hydrate, hydrateNode] as const
644648
}

0 commit comments

Comments
 (0)