feat(portal): SSR support for multi portal shared target

This commit is contained in:
Evan You 2020-03-27 20:49:01 -04:00
parent aafb880a0a
commit e866434f0c
7 changed files with 130 additions and 32 deletions

View File

@ -7,7 +7,7 @@ describe('ssr compile: portal', () => {
"const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\") "const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\")
return function ssrRender(_ctx, _push, _parent) { return function ssrRender(_ctx, _push, _parent) {
_ssrRenderPortal((_push) => { _ssrRenderPortal(_push, (_push) => {
_push(\`<div></div>\`) _push(\`<div></div>\`)
}, _ctx.target, _parent) }, _ctx.target, _parent)
}" }"

View File

@ -52,6 +52,7 @@ export function ssrProcessPortal(
contentRenderFn.body = processChildrenAsStatement(node.children, context) contentRenderFn.body = processChildrenAsStatement(node.children, context)
context.pushStatement( context.pushStatement(
createCallExpression(context.helper(SSR_RENDER_PORTAL), [ createCallExpression(context.helper(SSR_RENDER_PORTAL), [
`_push`,
contentRenderFn, contentRenderFn,
target, target,
`_parent` `_parent`

View File

@ -12,6 +12,7 @@ import {
} from '@vue/runtime-dom' } from '@vue/runtime-dom'
import { renderToString } from '@vue/server-renderer' import { renderToString } from '@vue/server-renderer'
import { mockWarn } from '@vue/shared' import { mockWarn } from '@vue/shared'
import { SSRContext } from 'packages/server-renderer/src/renderToString'
function mountWithHydration(html: string, render: () => any) { function mountWithHydration(html: string, render: () => any) {
const container = document.createElement('div') const container = document.createElement('div')
@ -157,7 +158,7 @@ describe('SSR hydration', () => {
const fn = jest.fn() const fn = jest.fn()
const portalContainer = document.createElement('div') const portalContainer = document.createElement('div')
portalContainer.id = 'portal' portalContainer.id = 'portal'
portalContainer.innerHTML = `<span>foo</span><span class="foo"></span>` portalContainer.innerHTML = `<span>foo</span><span class="foo"></span><!---->`
document.body.appendChild(portalContainer) document.body.appendChild(portalContainer)
const { vnode, container } = mountWithHydration('<!--portal-->', () => const { vnode, container } = mountWithHydration('<!--portal-->', () =>
@ -182,7 +183,69 @@ describe('SSR hydration', () => {
msg.value = 'bar' msg.value = 'bar'
await nextTick() await nextTick()
expect(portalContainer.innerHTML).toBe( expect(portalContainer.innerHTML).toBe(
`<span>bar</span><span class="bar"></span>` `<span>bar</span><span class="bar"></span><!---->`
)
})
test('Portal (multiple + integration)', async () => {
const msg = ref('foo')
const fn1 = jest.fn()
const fn2 = jest.fn()
const Comp = () => [
h(Portal, { target: '#portal2' }, [
h('span', msg.value),
h('span', { class: msg.value, onClick: fn1 })
]),
h(Portal, { target: '#portal2' }, [
h('span', msg.value + '2'),
h('span', { class: msg.value + '2', onClick: fn2 })
])
]
const portalContainer = document.createElement('div')
portalContainer.id = 'portal2'
const ctx: SSRContext = {}
const mainHtml = await renderToString(h(Comp), ctx)
expect(mainHtml).toMatchInlineSnapshot(
`"<!--[--><!--portal--><!--portal--><!--]-->"`
)
const portalHtml = ctx.portals!['#portal2']
expect(portalHtml).toMatchInlineSnapshot(
`"<span>foo</span><span class=\\"foo\\"></span><!----><span>foo2</span><span class=\\"foo2\\"></span><!---->"`
)
portalContainer.innerHTML = portalHtml
document.body.appendChild(portalContainer)
const { vnode, container } = mountWithHydration(mainHtml, Comp)
expect(vnode.el).toBe(container.firstChild)
const portalVnode1 = (vnode.children as VNode[])[0]
const portalVnode2 = (vnode.children as VNode[])[1]
expect(portalVnode1.el).toBe(container.childNodes[1])
expect(portalVnode2.el).toBe(container.childNodes[2])
expect((portalVnode1 as any).children[0].el).toBe(
portalContainer.childNodes[0]
)
expect(portalVnode1.anchor).toBe(portalContainer.childNodes[2])
expect((portalVnode2 as any).children[0].el).toBe(
portalContainer.childNodes[3]
)
expect(portalVnode2.anchor).toBe(portalContainer.childNodes[5])
// // event handler
triggerEvent('click', portalContainer.querySelector('.foo')!)
expect(fn1).toHaveBeenCalled()
triggerEvent('click', portalContainer.querySelector('.foo2')!)
expect(fn2).toHaveBeenCalled()
msg.value = 'bar'
await nextTick()
expect(portalContainer.innerHTML).toMatchInlineSnapshot(
`"<span>bar</span><span class=\\"bar\\"></span><!----><span>bar2</span><span class=\\"bar2\\"></span><!---->"`
) )
}) })

View File

@ -366,6 +366,11 @@ export function createHydrationFunctions(
} }
} }
interface PortalTargetElement extends Element {
// last portal target
_lpa?: Node | null
}
const hydratePortal = ( const hydratePortal = (
vnode: VNode, vnode: VNode,
parentComponent: ComponentInternalInstance | null, parentComponent: ComponentInternalInstance | null,
@ -377,14 +382,17 @@ export function createHydrationFunctions(
? document.querySelector(targetSelector) ? document.querySelector(targetSelector)
: targetSelector) : targetSelector)
if (target && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) { if (target && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
hydrateChildren( vnode.anchor = hydrateChildren(
target.firstChild, // if multiple portals rendered to the same target element, we need to
// pick up from where the last portal finished instead of the first node
(target as PortalTargetElement)._lpa || target.firstChild,
vnode, vnode,
target, target,
parentComponent, parentComponent,
parentSuspense, parentSuspense,
optimized optimized
) )
;(target as PortalTargetElement)._lpa = nextSibling(vnode.anchor as Node)
} else if (__DEV__) { } else if (__DEV__) {
warn( warn(
`Attempting to hydrate portal but target ${targetSelector} does not ` + `Attempting to hydrate portal but target ${targetSelector} does not ` +

View File

@ -4,16 +4,15 @@ import { ssrRenderPortal } from '../src/helpers/ssrRenderPortal'
describe('ssrRenderPortal', () => { describe('ssrRenderPortal', () => {
test('portal rendering (compiled)', async () => { test('portal rendering (compiled)', async () => {
const ctx = { const ctx: SSRContext = {}
portals: {} const html = await renderToString(
} as SSRContext
await renderToString(
createApp({ createApp({
data() { data() {
return { msg: 'hello' } return { msg: 'hello' }
}, },
ssrRender(_ctx, _push, _parent) { ssrRender(_ctx, _push, _parent) {
ssrRenderPortal( ssrRenderPortal(
_push,
_push => { _push => {
_push(`<div>content</div>`) _push(`<div>content</div>`)
}, },
@ -24,12 +23,13 @@ describe('ssrRenderPortal', () => {
}), }),
ctx ctx
) )
expect(ctx.portals!['#target']).toBe(`<div>content</div>`) expect(html).toBe('<!--portal-->')
expect(ctx.portals!['#target']).toBe(`<div>content</div><!---->`)
}) })
test('portal rendering (vnode)', async () => { test('portal rendering (vnode)', async () => {
const ctx: SSRContext = {} const ctx: SSRContext = {}
await renderToString( const html = await renderToString(
h( h(
Portal, Portal,
{ {
@ -39,6 +39,28 @@ describe('ssrRenderPortal', () => {
), ),
ctx ctx
) )
expect(ctx.portals!['#target']).toBe('<span>hello</span>') expect(html).toBe('<!--portal-->')
expect(ctx.portals!['#target']).toBe('<span>hello</span><!---->')
})
test('multiple portals with same target', async () => {
const ctx: SSRContext = {}
const html = await renderToString(
h('div', [
h(
Portal,
{
target: `#target`
},
h('span', 'hello')
),
h(Portal, { target: `#target` }, 'world')
]),
ctx
)
expect(html).toBe('<div><!--portal--><!--portal--></div>')
expect(ctx.portals!['#target']).toBe(
'<span>hello</span><!---->world<!---->'
)
}) })
}) })

View File

@ -2,19 +2,24 @@ import { ComponentInternalInstance, ssrContextKey } from 'vue'
import { SSRContext, createBuffer, PushFn } from '../renderToString' import { SSRContext, createBuffer, PushFn } from '../renderToString'
export function ssrRenderPortal( export function ssrRenderPortal(
parentPush: PushFn,
contentRenderFn: (push: PushFn) => void, contentRenderFn: (push: PushFn) => void,
target: string, target: string,
parentComponent: ComponentInternalInstance parentComponent: ComponentInternalInstance
) { ) {
parentPush('<!--portal-->')
const { getBuffer, push } = createBuffer() const { getBuffer, push } = createBuffer()
contentRenderFn(push) contentRenderFn(push)
push(`<!---->`) // portal end anchor
const context = parentComponent.appContext.provides[ const context = parentComponent.appContext.provides[
ssrContextKey as any ssrContextKey as any
] as SSRContext ] as SSRContext
const portalBuffers = const portalBuffers =
context.__portalBuffers || (context.__portalBuffers = {}) context.__portalBuffers || (context.__portalBuffers = {})
if (portalBuffers[target]) {
portalBuffers[target] = getBuffer() portalBuffers[target].push(getBuffer())
} else {
portalBuffers[target] = [getBuffer()]
}
} }

View File

@ -32,6 +32,7 @@ import { compile } from '@vue/compiler-ssr'
import { ssrRenderAttrs } from './helpers/ssrRenderAttrs' import { ssrRenderAttrs } from './helpers/ssrRenderAttrs'
import { SSRSlots } from './helpers/ssrRenderSlot' import { SSRSlots } from './helpers/ssrRenderSlot'
import { CompilerError } from '@vue/compiler-dom' import { CompilerError } from '@vue/compiler-dom'
import { ssrRenderPortal } from './helpers/ssrRenderPortal'
const { const {
isVNode, isVNode,
@ -63,10 +64,7 @@ export type Props = Record<string, unknown>
export type SSRContext = { export type SSRContext = {
[key: string]: any [key: string]: any
portals?: Record<string, string> portals?: Record<string, string>
__portalBuffers?: Record< __portalBuffers?: Record<string, SSRBuffer>
string,
ResolvedSSRBuffer | Promise<ResolvedSSRBuffer>
>
} }
export function createBuffer() { export function createBuffer() {
@ -259,7 +257,7 @@ function renderVNode(
} else if (shapeFlag & ShapeFlags.COMPONENT) { } else if (shapeFlag & ShapeFlags.COMPONENT) {
push(renderComponentVNode(vnode, parentComponent)) push(renderComponentVNode(vnode, parentComponent))
} else if (shapeFlag & ShapeFlags.PORTAL) { } else if (shapeFlag & ShapeFlags.PORTAL) {
renderPortalVNode(vnode, parentComponent) renderPortalVNode(push, vnode, parentComponent)
} else if (shapeFlag & ShapeFlags.SUSPENSE) { } else if (shapeFlag & ShapeFlags.SUSPENSE) {
renderVNode( renderVNode(
push, push,
@ -363,6 +361,7 @@ function applySSRDirectives(
} }
function renderPortalVNode( function renderPortalVNode(
push: PushFn,
vnode: VNode, vnode: VNode,
parentComponent: ComponentInternalInstance parentComponent: ComponentInternalInstance
) { ) {
@ -377,20 +376,18 @@ function renderPortalVNode(
) )
return [] return []
} }
ssrRenderPortal(
const { getBuffer, push } = createBuffer() push,
push => {
renderVNodeChildren( renderVNodeChildren(
push, push,
vnode.children as VNodeArrayChildren, vnode.children as VNodeArrayChildren,
parentComponent parentComponent
) )
const context = parentComponent.appContext.provides[ },
ssrContextKey as any target,
] as SSRContext parentComponent
const portalBuffers = )
context.__portalBuffers || (context.__portalBuffers = {})
portalBuffers[target] = getBuffer()
} }
async function resolvePortals(context: SSRContext) { async function resolvePortals(context: SSRContext) {
@ -399,7 +396,9 @@ async function resolvePortals(context: SSRContext) {
for (const key in context.__portalBuffers) { for (const key in context.__portalBuffers) {
// note: it's OK to await sequentially here because the Promises were // note: it's OK to await sequentially here because the Promises were
// created eagerly in parallel. // created eagerly in parallel.
context.portals[key] = unrollBuffer(await context.__portalBuffers[key]) context.portals[key] = unrollBuffer(
await Promise.all(context.__portalBuffers[key])
)
} }
} }
} }