Skip to content

Commit

Permalink
feat(ssr): support portal hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 15, 2020
1 parent 688ad92 commit 70dc3e3
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { queuePostFlushCb, flushPostFlushCbs } from './scheduler'
import { ComponentInternalInstance } from './component'
import { invokeDirectiveHook } from './directives'
import { warn } from './warning'
import { PatchFlags, ShapeFlags, isReservedProp, isOn } from '@vue/shared'
import {
PatchFlags,
ShapeFlags,
isReservedProp,
isOn,
isString
} from '@vue/shared'
import { RendererOptions, MountComponentFn } from './renderer'

export type RootHydrateFunction = (
Expand All @@ -38,8 +44,6 @@ export function createHydrationFunctions(
}

// TODO handle mismatches
// TODO SVG
// TODO Suspense
const hydrateNode = (
node: Node,
vnode: VNode,
Expand All @@ -62,8 +66,8 @@ export function createHydrationFunctions(
// back anchor as expected.
return anchor.nextSibling
case Portal:
// TODO
break
hydratePortal(vnode, parentComponent)
return node.nextSibling
default:
if (shapeFlag & ShapeFlags.ELEMENT) {
return hydrateElement(node as Element, vnode, parentComponent)
Expand All @@ -75,7 +79,7 @@ export function createHydrationFunctions(
const subTree = vnode.component!.subTree
return (subTree.anchor || subTree.el).nextSibling
} else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
// TODO
// TODO Suspense
} else if (__DEV__) {
warn('Invalid HostVNode type:', type, `(${typeof type})`)
}
Expand Down Expand Up @@ -147,5 +151,22 @@ export function createHydrationFunctions(
return node
}

const hydratePortal = (
vnode: VNode,
parentComponent: ComponentInternalInstance | null
) => {
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) {
hydrateChildren(
target.firstChild,
vnode.children as VNode[],
parentComponent
)
}
}

return [hydrate, hydrateNode] as const
}

0 comments on commit 70dc3e3

Please sign in to comment.