2020-03-18 23:43:32 +08:00
|
|
|
import {
|
|
|
|
VNode,
|
|
|
|
normalizeVNode,
|
|
|
|
Text,
|
|
|
|
Comment,
|
|
|
|
Static,
|
|
|
|
Fragment,
|
|
|
|
VNodeHook
|
|
|
|
} from './vnode'
|
2020-03-13 10:19:41 +08:00
|
|
|
import { flushPostFlushCbs } from './scheduler'
|
2020-03-30 23:23:59 +08:00
|
|
|
import { ComponentOptions, ComponentInternalInstance } from './component'
|
2020-02-14 12:31:03 +08:00
|
|
|
import { invokeDirectiveHook } from './directives'
|
|
|
|
import { warn } from './warning'
|
2020-03-30 23:23:59 +08:00
|
|
|
import { PatchFlags, ShapeFlags, isReservedProp, isOn } from '@vue/shared'
|
2020-05-22 05:37:23 +08:00
|
|
|
import { RendererInternals, invokeVNodeHook, setRef } from './renderer'
|
2020-03-13 10:19:41 +08:00
|
|
|
import {
|
|
|
|
SuspenseImpl,
|
|
|
|
SuspenseBoundary,
|
|
|
|
queueEffectWithSuspense
|
|
|
|
} from './components/Suspense'
|
2020-03-31 22:52:42 +08:00
|
|
|
import { TeleportImpl } from './components/Teleport'
|
2020-02-14 12:31:03 +08:00
|
|
|
|
2020-02-15 01:33:32 +08:00
|
|
|
export type RootHydrateFunction = (
|
|
|
|
vnode: VNode<Node, Element>,
|
|
|
|
container: Element
|
|
|
|
) => void
|
|
|
|
|
2020-03-04 05:12:38 +08:00
|
|
|
const enum DOMNodeTypes {
|
|
|
|
ELEMENT = 1,
|
|
|
|
TEXT = 3,
|
|
|
|
COMMENT = 8
|
|
|
|
}
|
|
|
|
|
2020-03-05 07:06:50 +08:00
|
|
|
let hasMismatch = false
|
2020-03-04 05:12:38 +08:00
|
|
|
|
2020-03-13 10:19:41 +08:00
|
|
|
const isSVGContainer = (container: Element) =>
|
|
|
|
/svg/.test(container.namespaceURI!) && container.tagName !== 'foreignObject'
|
|
|
|
|
|
|
|
const isComment = (node: Node): node is Comment =>
|
|
|
|
node.nodeType === DOMNodeTypes.COMMENT
|
|
|
|
|
2020-02-14 12:31:03 +08:00
|
|
|
// Note: hydration is DOM-specific
|
2020-02-14 14:30:08 +08:00
|
|
|
// But we have to place it in core due to tight coupling with core - splitting
|
2020-02-14 13:13:54 +08:00
|
|
|
// it out creates a ton of unnecessary complexity.
|
2020-02-14 14:30:08 +08:00
|
|
|
// Hydration also depends on some renderer internal logic which needs to be
|
|
|
|
// passed in via arguments.
|
2020-03-13 10:19:41 +08:00
|
|
|
export function createHydrationFunctions(
|
|
|
|
rendererInternals: RendererInternals<Node, Element>
|
|
|
|
) {
|
|
|
|
const {
|
|
|
|
mt: mountComponent,
|
|
|
|
p: patch,
|
2020-03-14 06:02:53 +08:00
|
|
|
o: { patchProp, nextSibling, parentNode, remove, insert, createComment }
|
2020-03-13 10:19:41 +08:00
|
|
|
} = rendererInternals
|
|
|
|
|
2020-02-15 01:33:32 +08:00
|
|
|
const hydrate: RootHydrateFunction = (vnode, container) => {
|
2020-02-14 12:31:03 +08:00
|
|
|
if (__DEV__ && !container.hasChildNodes()) {
|
2020-03-04 05:12:38 +08:00
|
|
|
warn(
|
|
|
|
`Attempting to hydrate existing markup but container is empty. ` +
|
|
|
|
`Performing full mount instead.`
|
|
|
|
)
|
|
|
|
patch(null, vnode, container)
|
2020-02-14 12:31:03 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = false
|
2020-03-13 10:19:41 +08:00
|
|
|
hydrateNode(container.firstChild!, vnode, null, null)
|
2020-02-14 12:31:03 +08:00
|
|
|
flushPostFlushCbs()
|
2020-03-05 07:06:50 +08:00
|
|
|
if (hasMismatch && !__TEST__) {
|
2020-03-04 05:12:38 +08:00
|
|
|
// this error should show up in production
|
|
|
|
console.error(`Hydration completed but contains mismatches.`)
|
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 16:22:52 +08:00
|
|
|
const hydrateNode = (
|
2020-02-14 12:31:03 +08:00
|
|
|
node: Node,
|
|
|
|
vnode: VNode,
|
2020-03-13 10:19:41 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
parentSuspense: SuspenseBoundary | null,
|
2020-02-28 00:26:39 +08:00
|
|
|
optimized = false
|
2020-02-27 05:32:06 +08:00
|
|
|
): Node | null => {
|
2020-03-13 23:55:04 +08:00
|
|
|
const isFragmentStart = isComment(node) && node.data === '['
|
2020-03-14 06:02:53 +08:00
|
|
|
const onMismatch = () =>
|
|
|
|
handleMismtach(
|
|
|
|
node,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
isFragmentStart
|
|
|
|
)
|
2020-03-13 23:35:17 +08:00
|
|
|
|
2020-05-22 05:37:23 +08:00
|
|
|
const { type, ref, shapeFlag } = vnode
|
2020-03-04 05:12:38 +08:00
|
|
|
const domType = node.nodeType
|
2020-02-14 12:31:03 +08:00
|
|
|
vnode.el = node
|
2020-03-04 05:12:38 +08:00
|
|
|
|
2020-05-22 05:37:23 +08:00
|
|
|
let nextNode: Node | null = null
|
2020-02-14 12:31:03 +08:00
|
|
|
switch (type) {
|
|
|
|
case Text:
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.TEXT) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = onMismatch()
|
|
|
|
} else {
|
|
|
|
if ((node as Text).data !== vnode.children) {
|
|
|
|
hasMismatch = true
|
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`Hydration text mismatch:` +
|
|
|
|
`\n- Client: ${JSON.stringify((node as Text).data)}` +
|
|
|
|
`\n- Server: ${JSON.stringify(vnode.children)}`
|
|
|
|
)
|
|
|
|
;(node as Text).data = vnode.children as string
|
|
|
|
}
|
|
|
|
nextNode = nextSibling(node)
|
2020-03-04 05:12:38 +08:00
|
|
|
}
|
2020-05-22 05:37:23 +08:00
|
|
|
break
|
2020-02-14 12:31:03 +08:00
|
|
|
case Comment:
|
2020-03-14 06:02:53 +08:00
|
|
|
if (domType !== DOMNodeTypes.COMMENT || isFragmentStart) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = onMismatch()
|
|
|
|
} else {
|
|
|
|
nextNode = nextSibling(node)
|
2020-03-04 05:12:38 +08:00
|
|
|
}
|
2020-05-22 05:37:23 +08:00
|
|
|
break
|
2020-02-14 12:31:03 +08:00
|
|
|
case Static:
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.ELEMENT) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = onMismatch()
|
|
|
|
} else {
|
|
|
|
// determine anchor, adopt content
|
|
|
|
nextNode = node
|
|
|
|
// if the static vnode has its content stripped during build,
|
|
|
|
// adopt it from the server-rendered HTML.
|
|
|
|
const needToAdoptContent = !(vnode.children as string).length
|
|
|
|
for (let i = 0; i < vnode.staticCount; i++) {
|
|
|
|
if (needToAdoptContent)
|
|
|
|
vnode.children += (nextNode as Element).outerHTML
|
|
|
|
if (i === vnode.staticCount - 1) {
|
|
|
|
vnode.anchor = nextNode
|
|
|
|
}
|
|
|
|
nextNode = nextSibling(nextNode)!
|
2020-05-16 04:30:20 +08:00
|
|
|
}
|
2020-05-22 05:37:23 +08:00
|
|
|
return nextNode
|
2020-05-16 04:30:20 +08:00
|
|
|
}
|
2020-05-22 05:37:23 +08:00
|
|
|
break
|
2020-02-14 12:31:03 +08:00
|
|
|
case Fragment:
|
2020-03-14 06:02:53 +08:00
|
|
|
if (!isFragmentStart) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = onMismatch()
|
|
|
|
} else {
|
|
|
|
nextNode = hydrateFragment(
|
|
|
|
node as Comment,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
optimized
|
|
|
|
)
|
2020-03-13 10:19:41 +08:00
|
|
|
}
|
2020-05-22 05:37:23 +08:00
|
|
|
break
|
2020-02-14 12:31:03 +08:00
|
|
|
default:
|
|
|
|
if (shapeFlag & ShapeFlags.ELEMENT) {
|
2020-03-07 04:39:54 +08:00
|
|
|
if (
|
|
|
|
domType !== DOMNodeTypes.ELEMENT ||
|
|
|
|
vnode.type !== (node as Element).tagName.toLowerCase()
|
|
|
|
) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = onMismatch()
|
|
|
|
} else {
|
|
|
|
nextNode = hydrateElement(
|
|
|
|
node as Element,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
optimized
|
|
|
|
)
|
2020-03-04 05:12:38 +08:00
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
} else if (shapeFlag & ShapeFlags.COMPONENT) {
|
2020-02-15 05:25:41 +08:00
|
|
|
// when setting up the render effect, if the initial vnode already
|
|
|
|
// has .el set, the component will perform hydration instead of mount
|
|
|
|
// on its sub-tree.
|
2020-03-13 10:19:41 +08:00
|
|
|
const container = parentNode(node)!
|
2020-03-24 04:14:56 +08:00
|
|
|
const hydrateComponent = () => {
|
|
|
|
mountComponent(
|
|
|
|
vnode,
|
|
|
|
container,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
2020-04-07 05:37:47 +08:00
|
|
|
isSVGContainer(container),
|
|
|
|
optimized
|
2020-03-24 04:14:56 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
// async component
|
|
|
|
const loadAsync = (vnode.type as ComponentOptions).__asyncLoader
|
|
|
|
if (loadAsync) {
|
|
|
|
loadAsync().then(hydrateComponent)
|
|
|
|
} else {
|
|
|
|
hydrateComponent()
|
|
|
|
}
|
2020-03-14 01:04:44 +08:00
|
|
|
// component may be async, so in the case of fragments we cannot rely
|
|
|
|
// on component's rendered output to determine the end of the fragment
|
|
|
|
// instead, we do a lookahead to find the end anchor node.
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = isFragmentStart
|
2020-03-14 01:04:44 +08:00
|
|
|
? locateClosingAsyncAnchor(node)
|
|
|
|
: nextSibling(node)
|
2020-03-31 22:52:42 +08:00
|
|
|
} else if (shapeFlag & ShapeFlags.TELEPORT) {
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.COMMENT) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = onMismatch()
|
|
|
|
} else {
|
|
|
|
nextNode = (vnode.type as typeof TeleportImpl).hydrate(
|
|
|
|
node,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
optimized,
|
|
|
|
rendererInternals,
|
|
|
|
hydrateChildren
|
|
|
|
)
|
2020-03-04 05:12:38 +08:00
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
} else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
|
2020-05-22 05:37:23 +08:00
|
|
|
nextNode = (vnode.type as typeof SuspenseImpl).hydrate(
|
2020-03-13 10:19:41 +08:00
|
|
|
node,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
isSVGContainer(parentNode(node)!),
|
|
|
|
optimized,
|
|
|
|
rendererInternals,
|
|
|
|
hydrateNode
|
|
|
|
)
|
2020-02-14 12:31:03 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn('Invalid HostVNode type:', type, `(${typeof type})`)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 05:37:23 +08:00
|
|
|
|
|
|
|
if (ref != null && parentComponent) {
|
|
|
|
setRef(ref, null, parentComponent, vnode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextNode
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 16:22:52 +08:00
|
|
|
const hydrateElement = (
|
2020-02-14 12:31:03 +08:00
|
|
|
el: Element,
|
|
|
|
vnode: VNode,
|
2020-02-28 00:26:39 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
2020-03-13 10:19:41 +08:00
|
|
|
parentSuspense: SuspenseBoundary | null,
|
2020-02-28 00:26:39 +08:00
|
|
|
optimized: boolean
|
2020-02-14 16:22:52 +08:00
|
|
|
) => {
|
2020-03-19 06:14:51 +08:00
|
|
|
optimized = optimized || !!vnode.dynamicChildren
|
2020-03-18 23:43:32 +08:00
|
|
|
const { props, patchFlag, shapeFlag, dirs } = vnode
|
2020-02-14 12:31:03 +08:00
|
|
|
// skip props & children if this is hoisted static nodes
|
|
|
|
if (patchFlag !== PatchFlags.HOISTED) {
|
|
|
|
// props
|
2020-03-19 06:14:51 +08:00
|
|
|
if (props) {
|
2020-02-14 12:31:03 +08:00
|
|
|
if (
|
2020-03-06 00:29:50 +08:00
|
|
|
!optimized ||
|
|
|
|
(patchFlag & PatchFlags.FULL_PROPS ||
|
|
|
|
patchFlag & PatchFlags.HYDRATE_EVENTS)
|
2020-02-14 12:31:03 +08:00
|
|
|
) {
|
|
|
|
for (const key in props) {
|
|
|
|
if (!isReservedProp(key) && isOn(key)) {
|
2020-03-10 04:15:49 +08:00
|
|
|
patchProp(el, key, null, props[key])
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 06:14:51 +08:00
|
|
|
} else if (props.onClick) {
|
2020-02-14 12:31:03 +08:00
|
|
|
// Fast path for click listeners (which is most often) to avoid
|
|
|
|
// iterating through props.
|
2020-03-10 04:15:49 +08:00
|
|
|
patchProp(el, 'onClick', null, props.onClick)
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
2020-03-18 23:43:32 +08:00
|
|
|
}
|
|
|
|
// vnode / directive hooks
|
|
|
|
let vnodeHooks: VNodeHook | null | undefined
|
2020-03-19 06:14:51 +08:00
|
|
|
if ((vnodeHooks = props && props.onVnodeBeforeMount)) {
|
2020-03-18 23:43:32 +08:00
|
|
|
invokeVNodeHook(vnodeHooks, parentComponent, vnode)
|
|
|
|
}
|
2020-03-19 06:14:51 +08:00
|
|
|
if (dirs) {
|
2020-03-18 23:43:32 +08:00
|
|
|
invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
|
|
|
|
}
|
2020-03-19 06:14:51 +08:00
|
|
|
if ((vnodeHooks = props && props.onVnodeMounted) || dirs) {
|
2020-03-18 23:43:32 +08:00
|
|
|
queueEffectWithSuspense(() => {
|
|
|
|
vnodeHooks && invokeVNodeHook(vnodeHooks, parentComponent, vnode)
|
|
|
|
dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted')
|
|
|
|
}, parentSuspense)
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
// children
|
|
|
|
if (
|
2020-03-04 05:12:38 +08:00
|
|
|
shapeFlag & ShapeFlags.ARRAY_CHILDREN &&
|
2020-02-14 12:31:03 +08:00
|
|
|
// skip if element has innerHTML / textContent
|
2020-03-19 06:14:51 +08:00
|
|
|
!(props && (props.innerHTML || props.textContent))
|
2020-02-14 12:31:03 +08:00
|
|
|
) {
|
2020-03-04 05:12:38 +08:00
|
|
|
let next = hydrateChildren(
|
2020-02-14 12:31:03 +08:00
|
|
|
el.firstChild,
|
2020-02-28 00:26:39 +08:00
|
|
|
vnode,
|
2020-03-04 05:12:38 +08:00
|
|
|
el,
|
2020-02-28 00:26:39 +08:00
|
|
|
parentComponent,
|
2020-03-13 10:19:41 +08:00
|
|
|
parentSuspense,
|
2020-03-06 00:29:50 +08:00
|
|
|
optimized
|
2020-02-14 12:31:03 +08:00
|
|
|
)
|
2020-03-07 04:39:54 +08:00
|
|
|
let hasWarned = false
|
2020-03-04 05:12:38 +08:00
|
|
|
while (next) {
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = true
|
2020-03-07 04:39:54 +08:00
|
|
|
if (__DEV__ && !hasWarned) {
|
2020-03-04 05:12:38 +08:00
|
|
|
warn(
|
2020-03-07 04:39:54 +08:00
|
|
|
`Hydration children mismatch in <${vnode.type as string}>: ` +
|
2020-03-04 05:12:38 +08:00
|
|
|
`server rendered element contains more child nodes than client vdom.`
|
|
|
|
)
|
2020-03-07 04:39:54 +08:00
|
|
|
hasWarned = true
|
|
|
|
}
|
2020-03-04 05:12:38 +08:00
|
|
|
// The SSRed DOM contains more nodes than it should. Remove them.
|
|
|
|
const cur = next
|
|
|
|
next = next.nextSibling
|
2020-03-14 06:02:53 +08:00
|
|
|
remove(cur)
|
2020-03-04 05:12:38 +08:00
|
|
|
}
|
|
|
|
} else if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
|
2020-03-07 04:39:54 +08:00
|
|
|
if (el.textContent !== vnode.children) {
|
|
|
|
hasMismatch = true
|
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`Hydration text content mismatch in <${vnode.type as string}>:\n` +
|
|
|
|
`- Client: ${el.textContent}\n` +
|
|
|
|
`- Server: ${vnode.children as string}`
|
|
|
|
)
|
|
|
|
el.textContent = vnode.children as string
|
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return el.nextSibling
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:22:52 +08:00
|
|
|
const hydrateChildren = (
|
2020-02-27 05:32:06 +08:00
|
|
|
node: Node | null,
|
2020-02-28 00:26:39 +08:00
|
|
|
vnode: VNode,
|
2020-03-04 05:12:38 +08:00
|
|
|
container: Element,
|
2020-02-28 00:26:39 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
2020-03-13 10:19:41 +08:00
|
|
|
parentSuspense: SuspenseBoundary | null,
|
2020-02-28 00:26:39 +08:00
|
|
|
optimized: boolean
|
2020-02-27 05:32:06 +08:00
|
|
|
): Node | null => {
|
2020-03-19 06:14:51 +08:00
|
|
|
optimized = optimized || !!vnode.dynamicChildren
|
2020-03-05 07:06:50 +08:00
|
|
|
const children = vnode.children as VNode[]
|
|
|
|
const l = children.length
|
2020-03-07 04:39:54 +08:00
|
|
|
let hasWarned = false
|
2020-03-05 07:06:50 +08:00
|
|
|
for (let i = 0; i < l; i++) {
|
2020-02-28 00:26:39 +08:00
|
|
|
const vnode = optimized
|
|
|
|
? children[i]
|
|
|
|
: (children[i] = normalizeVNode(children[i]))
|
2020-03-04 05:12:38 +08:00
|
|
|
if (node) {
|
2020-03-13 10:19:41 +08:00
|
|
|
node = hydrateNode(
|
|
|
|
node,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
optimized
|
|
|
|
)
|
2020-03-04 05:12:38 +08:00
|
|
|
} else {
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = true
|
2020-03-07 04:39:54 +08:00
|
|
|
if (__DEV__ && !hasWarned) {
|
2020-03-04 05:12:38 +08:00
|
|
|
warn(
|
2020-03-07 04:39:54 +08:00
|
|
|
`Hydration children mismatch in <${container.tagName.toLowerCase()}>: ` +
|
2020-03-04 05:12:38 +08:00
|
|
|
`server rendered element contains fewer child nodes than client vdom.`
|
|
|
|
)
|
2020-03-07 04:39:54 +08:00
|
|
|
hasWarned = true
|
|
|
|
}
|
2020-03-04 05:12:38 +08:00
|
|
|
// the SSRed DOM didn't contain enough nodes. Mount the missing ones.
|
2020-03-13 10:19:41 +08:00
|
|
|
patch(
|
|
|
|
null,
|
|
|
|
vnode,
|
|
|
|
container,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
isSVGContainer(container)
|
|
|
|
)
|
2020-03-04 05:12:38 +08:00
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2020-02-28 00:26:39 +08:00
|
|
|
const hydrateFragment = (
|
2020-03-13 10:19:41 +08:00
|
|
|
node: Comment,
|
2020-02-28 00:26:39 +08:00
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | null,
|
2020-03-13 10:19:41 +08:00
|
|
|
parentSuspense: SuspenseBoundary | null,
|
2020-02-28 00:26:39 +08:00
|
|
|
optimized: boolean
|
|
|
|
) => {
|
2020-03-13 23:35:17 +08:00
|
|
|
const container = parentNode(node)!
|
2020-03-14 06:02:53 +08:00
|
|
|
const next = hydrateChildren(
|
2020-03-13 23:35:17 +08:00
|
|
|
nextSibling(node)!,
|
|
|
|
vnode,
|
|
|
|
container,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
optimized
|
2020-03-14 06:02:53 +08:00
|
|
|
)
|
|
|
|
if (next && isComment(next) && next.data === ']') {
|
|
|
|
return nextSibling((vnode.anchor = next))
|
|
|
|
} else {
|
|
|
|
// fragment didn't hydrate successfully, since we didn't get a end anchor
|
|
|
|
// back. This should have led to node/children mismatch warnings.
|
|
|
|
hasMismatch = true
|
|
|
|
// since the anchor is missing, we need to create one and insert it
|
|
|
|
insert((vnode.anchor = createComment(`]`)), container, next)
|
|
|
|
return next
|
2020-03-13 23:35:17 +08:00
|
|
|
}
|
2020-02-28 00:26:39 +08:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:12:38 +08:00
|
|
|
const handleMismtach = (
|
|
|
|
node: Node,
|
|
|
|
vnode: VNode,
|
2020-03-13 10:19:41 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
2020-03-14 06:02:53 +08:00
|
|
|
parentSuspense: SuspenseBoundary | null,
|
|
|
|
isFragment: boolean
|
2020-05-22 05:37:23 +08:00
|
|
|
): Node | null => {
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = true
|
2020-03-04 05:12:38 +08:00
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`Hydration node mismatch:\n- Client vnode:`,
|
|
|
|
vnode.type,
|
|
|
|
`\n- Server rendered DOM:`,
|
2020-03-13 10:19:41 +08:00
|
|
|
node,
|
2020-03-14 06:02:53 +08:00
|
|
|
node.nodeType === DOMNodeTypes.TEXT
|
|
|
|
? `(text)`
|
|
|
|
: isComment(node) && node.data === '['
|
|
|
|
? `(start of fragment)`
|
|
|
|
: ``
|
2020-03-04 05:12:38 +08:00
|
|
|
)
|
|
|
|
vnode.el = null
|
2020-03-14 06:02:53 +08:00
|
|
|
|
|
|
|
if (isFragment) {
|
|
|
|
// remove excessive fragment nodes
|
|
|
|
const end = locateClosingAsyncAnchor(node)
|
|
|
|
while (true) {
|
|
|
|
const next = nextSibling(node)
|
|
|
|
if (next && next !== end) {
|
|
|
|
remove(next)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:19:41 +08:00
|
|
|
const next = nextSibling(node)
|
|
|
|
const container = parentNode(node)!
|
2020-03-14 06:02:53 +08:00
|
|
|
remove(node)
|
|
|
|
|
2020-03-13 10:19:41 +08:00
|
|
|
patch(
|
|
|
|
null,
|
|
|
|
vnode,
|
|
|
|
container,
|
|
|
|
next,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
isSVGContainer(container)
|
|
|
|
)
|
2020-03-04 05:12:38 +08:00
|
|
|
return next
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:19:41 +08:00
|
|
|
const locateClosingAsyncAnchor = (node: Node | null): Node | null => {
|
|
|
|
let match = 0
|
|
|
|
while (node) {
|
|
|
|
node = nextSibling(node)
|
|
|
|
if (node && isComment(node)) {
|
2020-03-13 23:55:04 +08:00
|
|
|
if (node.data === '[') match++
|
|
|
|
if (node.data === ']') {
|
2020-03-13 10:19:41 +08:00
|
|
|
if (match === 0) {
|
|
|
|
return nextSibling(node)
|
|
|
|
} else {
|
|
|
|
match--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:31:03 +08:00
|
|
|
return [hydrate, hydrateNode] as const
|
|
|
|
}
|