2020-02-16 00:40:09 +08:00
|
|
|
import { VNode, normalizeVNode, Text, Comment, Static, Fragment } from './vnode'
|
2020-02-14 12:31:03 +08:00
|
|
|
import { queuePostFlushCb, flushPostFlushCbs } from './scheduler'
|
|
|
|
import { ComponentInternalInstance } from './component'
|
|
|
|
import { invokeDirectiveHook } from './directives'
|
|
|
|
import { warn } from './warning'
|
2020-02-15 10:04:08 +08:00
|
|
|
import {
|
|
|
|
PatchFlags,
|
|
|
|
ShapeFlags,
|
|
|
|
isReservedProp,
|
|
|
|
isOn,
|
|
|
|
isString
|
|
|
|
} from '@vue/shared'
|
2020-02-16 00:40:09 +08:00
|
|
|
import { RendererInternals } from './renderer'
|
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-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-02-16 00:40:09 +08:00
|
|
|
export function createHydrationFunctions({
|
|
|
|
mt: mountComponent,
|
2020-03-04 05:12:38 +08:00
|
|
|
p: patch,
|
2020-02-27 05:32:06 +08:00
|
|
|
o: { patchProp, createText }
|
2020-02-16 00:40:09 +08:00
|
|
|
}: RendererInternals<Node, Element>) {
|
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-02-14 12:31:03 +08:00
|
|
|
hydrateNode(container.firstChild!, vnode)
|
|
|
|
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-02-28 00:26:39 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null = null,
|
|
|
|
optimized = false
|
2020-02-27 05:32:06 +08:00
|
|
|
): Node | null => {
|
2020-02-14 12:31:03 +08:00
|
|
|
const { type, 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-02-14 12:31:03 +08:00
|
|
|
switch (type) {
|
|
|
|
case Text:
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.TEXT) {
|
|
|
|
return handleMismtach(node, vnode, parentComponent)
|
|
|
|
}
|
|
|
|
if ((node as Text).data !== vnode.children) {
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = true
|
2020-03-04 05:12:38 +08:00
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`Hydration text mismatch:` +
|
|
|
|
`\n- Client: ${JSON.stringify(vnode.children)}`,
|
|
|
|
`\n- Server: ${JSON.stringify((node as Text).data)}`
|
|
|
|
)
|
|
|
|
;(node as Text).data = vnode.children as string
|
|
|
|
}
|
|
|
|
return node.nextSibling
|
2020-02-14 12:31:03 +08:00
|
|
|
case Comment:
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.COMMENT) {
|
|
|
|
return handleMismtach(node, vnode, parentComponent)
|
|
|
|
}
|
|
|
|
return node.nextSibling
|
2020-02-14 12:31:03 +08:00
|
|
|
case Static:
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.ELEMENT) {
|
|
|
|
return handleMismtach(node, vnode, parentComponent)
|
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
return node.nextSibling
|
|
|
|
case Fragment:
|
2020-02-28 00:26:39 +08:00
|
|
|
return hydrateFragment(node, vnode, parentComponent, optimized)
|
2020-02-14 12:31:03 +08:00
|
|
|
default:
|
|
|
|
if (shapeFlag & ShapeFlags.ELEMENT) {
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.ELEMENT) {
|
|
|
|
return handleMismtach(node, vnode, parentComponent)
|
|
|
|
}
|
2020-02-28 00:26:39 +08:00
|
|
|
return hydrateElement(
|
|
|
|
node as Element,
|
|
|
|
vnode,
|
|
|
|
parentComponent,
|
|
|
|
optimized
|
|
|
|
)
|
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-02-14 12:31:03 +08:00
|
|
|
mountComponent(vnode, null, null, parentComponent, null, false)
|
|
|
|
const subTree = vnode.component!.subTree
|
|
|
|
return (subTree.anchor || subTree.el).nextSibling
|
2020-02-16 00:40:09 +08:00
|
|
|
} else if (shapeFlag & ShapeFlags.PORTAL) {
|
2020-03-04 05:12:38 +08:00
|
|
|
if (domType !== DOMNodeTypes.COMMENT) {
|
|
|
|
return handleMismtach(node, vnode, parentComponent)
|
|
|
|
}
|
2020-03-05 07:06:50 +08:00
|
|
|
hydratePortal(vnode, parentComponent, optimized)
|
2020-02-16 00:40:09 +08:00
|
|
|
return node.nextSibling
|
2020-02-14 12:31:03 +08:00
|
|
|
} else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
|
2020-02-15 10:04:08 +08:00
|
|
|
// TODO Suspense
|
2020-02-14 12:31:03 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn('Invalid HostVNode type:', type, `(${typeof type})`)
|
|
|
|
}
|
2020-02-27 05:32:06 +08:00
|
|
|
return null
|
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,
|
|
|
|
optimized: boolean
|
2020-02-14 16:22:52 +08:00
|
|
|
) => {
|
2020-03-04 05:12:38 +08:00
|
|
|
const { props, patchFlag, shapeFlag } = vnode
|
2020-02-14 12:31:03 +08:00
|
|
|
// skip props & children if this is hoisted static nodes
|
|
|
|
if (patchFlag !== PatchFlags.HOISTED) {
|
|
|
|
// props
|
|
|
|
if (props !== null) {
|
|
|
|
if (
|
|
|
|
patchFlag & PatchFlags.FULL_PROPS ||
|
|
|
|
patchFlag & PatchFlags.HYDRATE_EVENTS
|
|
|
|
) {
|
|
|
|
for (const key in props) {
|
|
|
|
if (!isReservedProp(key) && isOn(key)) {
|
|
|
|
patchProp(el, key, props[key], null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (props.onClick != null) {
|
|
|
|
// Fast path for click listeners (which is most often) to avoid
|
|
|
|
// iterating through props.
|
|
|
|
patchProp(el, 'onClick', props.onClick, null)
|
|
|
|
}
|
|
|
|
// vnode hooks
|
|
|
|
const { onVnodeBeforeMount, onVnodeMounted } = props
|
|
|
|
if (onVnodeBeforeMount != null) {
|
|
|
|
invokeDirectiveHook(onVnodeBeforeMount, parentComponent, vnode)
|
|
|
|
}
|
|
|
|
if (onVnodeMounted != null) {
|
|
|
|
queuePostFlushCb(() => {
|
|
|
|
invokeDirectiveHook(onVnodeMounted, parentComponent, vnode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
!(props !== null && (props.innerHTML || props.textContent))
|
|
|
|
) {
|
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,
|
|
|
|
optimized || vnode.dynamicChildren !== null
|
2020-02-14 12:31:03 +08:00
|
|
|
)
|
2020-03-04 05:12:38 +08:00
|
|
|
while (next) {
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = true
|
2020-03-04 05:12:38 +08:00
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`Hydration children mismatch: ` +
|
|
|
|
`server rendered element contains more child nodes than client vdom.`
|
|
|
|
)
|
|
|
|
// The SSRed DOM contains more nodes than it should. Remove them.
|
|
|
|
const cur = next
|
|
|
|
next = next.nextSibling
|
|
|
|
el.removeChild(cur)
|
|
|
|
}
|
|
|
|
} else if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
|
|
|
|
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,
|
|
|
|
optimized: boolean
|
2020-02-27 05:32:06 +08:00
|
|
|
): Node | null => {
|
2020-02-28 00:26:39 +08:00
|
|
|
optimized = optimized || vnode.dynamicChildren !== null
|
2020-03-05 07:06:50 +08:00
|
|
|
const children = vnode.children as VNode[]
|
|
|
|
const l = children.length
|
|
|
|
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) {
|
|
|
|
node = hydrateNode(node, vnode, parentComponent, optimized)
|
|
|
|
} else {
|
2020-03-05 07:06:50 +08:00
|
|
|
hasMismatch = true
|
2020-03-04 05:12:38 +08:00
|
|
|
__DEV__ &&
|
|
|
|
warn(
|
|
|
|
`Hydration children mismatch: ` +
|
|
|
|
`server rendered element contains fewer child nodes than client vdom.`
|
|
|
|
)
|
|
|
|
// the SSRed DOM didn't contain enough nodes. Mount the missing ones.
|
|
|
|
patch(null, vnode, container)
|
|
|
|
}
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2020-02-28 00:26:39 +08:00
|
|
|
const hydrateFragment = (
|
|
|
|
node: Node,
|
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
optimized: boolean
|
|
|
|
) => {
|
2020-03-04 05:12:38 +08:00
|
|
|
const parent = node.parentNode as Element
|
2020-02-28 00:26:39 +08:00
|
|
|
parent.insertBefore((vnode.el = createText('')), node)
|
2020-03-04 05:12:38 +08:00
|
|
|
const next = hydrateChildren(
|
|
|
|
node,
|
|
|
|
vnode,
|
|
|
|
parent,
|
|
|
|
parentComponent,
|
|
|
|
optimized
|
|
|
|
)
|
2020-02-28 00:26:39 +08:00
|
|
|
parent.insertBefore((vnode.anchor = createText('')), next)
|
|
|
|
return next
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:04:08 +08:00
|
|
|
const hydratePortal = (
|
|
|
|
vnode: VNode,
|
2020-02-28 00:26:39 +08:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
optimized: boolean
|
2020-02-15 10:04:08 +08:00
|
|
|
) => {
|
|
|
|
const targetSelector = vnode.props && vnode.props.target
|
|
|
|
const target = (vnode.target = isString(targetSelector)
|
|
|
|
? document.querySelector(targetSelector)
|
|
|
|
: targetSelector)
|
|
|
|
if (target != null && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
|
2020-03-04 05:12:38 +08:00
|
|
|
hydrateChildren(
|
|
|
|
target.firstChild,
|
|
|
|
vnode,
|
2020-03-05 07:06:50 +08:00
|
|
|
target,
|
2020-03-04 05:12:38 +08:00
|
|
|
parentComponent,
|
|
|
|
optimized
|
|
|
|
)
|
2020-03-05 07:06:50 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`Attempting to hydrate portal but target ${targetSelector} does not ` +
|
|
|
|
`exist in server-rendered markup.`
|
|
|
|
)
|
2020-02-15 10:04:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:12:38 +08:00
|
|
|
const handleMismtach = (
|
|
|
|
node: Node,
|
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | 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:`,
|
|
|
|
node
|
|
|
|
)
|
|
|
|
vnode.el = null
|
|
|
|
const next = node.nextSibling
|
|
|
|
const container = node.parentNode as Element
|
|
|
|
container.removeChild(node)
|
|
|
|
// TODO Suspense and SVG
|
|
|
|
patch(null, vnode, container, next, parentComponent)
|
|
|
|
return next
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:31:03 +08:00
|
|
|
return [hydrate, hydrateNode] as const
|
|
|
|
}
|