Skip to content

Commit

Permalink
feat(ssr): hide comment anchors during hydration in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 13, 2020
1 parent a3cc970 commit cad5bcc
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ export function createHydrationFunctions(
parentSuspense: SuspenseBoundary | null,
optimized = false
): Node | null => {
const isFragmentStart = isComment(node) && node.data === '1'
if (__DEV__ && isFragmentStart) {
// in dev mode, replace comment anchors with invisible text nodes
// for easier debugging
node = replaceAnchor(node, parentNode(node)!)
}

const { type, shapeFlag } = vnode
const domType = node.nodeType

vnode.el = node

switch (type) {
Expand Down Expand Up @@ -108,7 +114,7 @@ export function createHydrationFunctions(
}
return nextSibling(node)
case Fragment:
if (domType !== DOMNodeTypes.COMMENT) {
if (domType !== (__DEV__ ? DOMNodeTypes.TEXT : DOMNodeTypes.COMMENT)) {
return handleMismtach(node, vnode, parentComponent, parentSuspense)
}
return hydrateFragment(
Expand Down Expand Up @@ -152,7 +158,7 @@ export function createHydrationFunctions(
} else {
// no subTree means this is an async component
// try to locate the ending node
return isComment(node) && node.data === '1'
return isFragmentStart
? locateClosingAsyncAnchor(node)
: nextSibling(node)
}
Expand Down Expand Up @@ -319,16 +325,19 @@ export function createHydrationFunctions(
parentSuspense: SuspenseBoundary | null,
optimized: boolean
) => {
return nextSibling(
(vnode.anchor = hydrateChildren(
nextSibling(node)!,
vnode,
parentNode(node)!,
parentComponent,
parentSuspense,
optimized
)!)
)
const container = parentNode(node)!
let next = hydrateChildren(
nextSibling(node)!,
vnode,
container,
parentComponent,
parentSuspense,
optimized
)!
if (__DEV__) {
next = replaceAnchor(next, container)
}
return nextSibling((vnode.anchor = next))
}

const hydratePortal = (
Expand Down Expand Up @@ -377,7 +386,6 @@ export function createHydrationFunctions(
const next = nextSibling(node)
const container = parentNode(node)!
container.removeChild(node)
// TODO Suspense
patch(
null,
vnode,
Expand Down Expand Up @@ -408,5 +416,12 @@ export function createHydrationFunctions(
return node
}

const replaceAnchor = (node: Node, parent: Element): Node => {
const text = document.createTextNode('')
parent.insertBefore(text, node)
parent.removeChild(node)
return text
}

return [hydrate, hydrateNode] as const
}

0 comments on commit cad5bcc

Please sign in to comment.