Skip to content

Commit cad5bcc

Browse files
committed
feat(ssr): hide comment anchors during hydration in dev mode
1 parent a3cc970 commit cad5bcc

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

packages/runtime-core/src/hydration.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ export function createHydrationFunctions(
7676
parentSuspense: SuspenseBoundary | null,
7777
optimized = false
7878
): Node | null => {
79+
const isFragmentStart = isComment(node) && node.data === '1'
80+
if (__DEV__ && isFragmentStart) {
81+
// in dev mode, replace comment anchors with invisible text nodes
82+
// for easier debugging
83+
node = replaceAnchor(node, parentNode(node)!)
84+
}
85+
7986
const { type, shapeFlag } = vnode
8087
const domType = node.nodeType
81-
8288
vnode.el = node
8389

8490
switch (type) {
@@ -108,7 +114,7 @@ export function createHydrationFunctions(
108114
}
109115
return nextSibling(node)
110116
case Fragment:
111-
if (domType !== DOMNodeTypes.COMMENT) {
117+
if (domType !== (__DEV__ ? DOMNodeTypes.TEXT : DOMNodeTypes.COMMENT)) {
112118
return handleMismtach(node, vnode, parentComponent, parentSuspense)
113119
}
114120
return hydrateFragment(
@@ -152,7 +158,7 @@ export function createHydrationFunctions(
152158
} else {
153159
// no subTree means this is an async component
154160
// try to locate the ending node
155-
return isComment(node) && node.data === '1'
161+
return isFragmentStart
156162
? locateClosingAsyncAnchor(node)
157163
: nextSibling(node)
158164
}
@@ -319,16 +325,19 @@ export function createHydrationFunctions(
319325
parentSuspense: SuspenseBoundary | null,
320326
optimized: boolean
321327
) => {
322-
return nextSibling(
323-
(vnode.anchor = hydrateChildren(
324-
nextSibling(node)!,
325-
vnode,
326-
parentNode(node)!,
327-
parentComponent,
328-
parentSuspense,
329-
optimized
330-
)!)
331-
)
328+
const container = parentNode(node)!
329+
let next = hydrateChildren(
330+
nextSibling(node)!,
331+
vnode,
332+
container,
333+
parentComponent,
334+
parentSuspense,
335+
optimized
336+
)!
337+
if (__DEV__) {
338+
next = replaceAnchor(next, container)
339+
}
340+
return nextSibling((vnode.anchor = next))
332341
}
333342

334343
const hydratePortal = (
@@ -377,7 +386,6 @@ export function createHydrationFunctions(
377386
const next = nextSibling(node)
378387
const container = parentNode(node)!
379388
container.removeChild(node)
380-
// TODO Suspense
381389
patch(
382390
null,
383391
vnode,
@@ -408,5 +416,12 @@ export function createHydrationFunctions(
408416
return node
409417
}
410418

419+
const replaceAnchor = (node: Node, parent: Element): Node => {
420+
const text = document.createTextNode('')
421+
parent.insertBefore(text, node)
422+
parent.removeChild(node)
423+
return text
424+
}
425+
411426
return [hydrate, hydrateNode] as const
412427
}

0 commit comments

Comments
 (0)