Skip to content

Commit

Permalink
refactor: rebase on master + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 9, 2020
1 parent a79a307 commit 4ab6ea6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ function createSuspenseBoundary<HostNode, HostElement>(
return suspense
}

function normalizeSuspenseChildren(
export function normalizeSuspenseChildren(
vnode: VNode
): {
content: VNode
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import {
setCurrentRenderingInstance
} from './componentRenderUtils'
import { isVNode, normalizeVNode } from './vnode'
import { normalizeSuspenseChildren } from './components/Suspense'

// SSR utils are only exposed in cjs builds.
const _ssrUtils = {
Expand All @@ -122,7 +123,8 @@ const _ssrUtils = {
renderComponentRoot,
setCurrentRenderingInstance,
isVNode,
normalizeVNode
normalizeVNode,
normalizeSuspenseChildren
}

export const ssrUtils = (__NODE_JS__ ? _ssrUtils : null) as typeof _ssrUtils
Expand Down
56 changes: 19 additions & 37 deletions packages/server-renderer/src/renderToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const {
setCurrentRenderingInstance,
setupComponent,
renderComponentRoot,
normalizeVNode
normalizeVNode,
normalizeSuspenseChildren
} = ssrUtils

// Each component has a buffer array.
Expand Down Expand Up @@ -224,27 +225,6 @@ function ssrCompile(
return (compileCache[template] = Function('require', code)(require))
}

function normalizeSuspenseChildren(
vnode: VNode
): {
content: VNode
fallback: VNode
} {
const { shapeFlag, children } = vnode
if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
const { default: d, fallback } = children as Slots
return {
content: normalizeVNode(isFunction(d) ? d() : d),
fallback: normalizeVNode(isFunction(fallback) ? fallback() : fallback)
}
} else {
return {
content: normalizeVNode(children as any),
fallback: normalizeVNode(null)
}
}
}

function renderVNode(
push: PushFn,
vnode: VNode,
Expand All @@ -269,21 +249,7 @@ function renderVNode(
} else if (shapeFlag & ShapeFlags.PORTAL) {
renderPortal(vnode, parentComponent)
} else if (shapeFlag & ShapeFlags.SUSPENSE) {
const { content, fallback } = normalizeSuspenseChildren(vnode)

push(
(async () => {
try {
const suspenseBuffer = createBuffer()
renderVNode(suspenseBuffer.push, content, parentComponent)
return await Promise.all(suspenseBuffer.buffer)
} catch {
const fallbackBuffer = createBuffer()
renderVNode(fallbackBuffer.push, fallback, parentComponent)
return await Promise.all(fallbackBuffer.buffer)
}
})()
)
push(renderSuspense(vnode, parentComponent))
} else {
console.warn(
'[@vue/server-renderer] Invalid VNode type:',
Expand Down Expand Up @@ -400,3 +366,19 @@ async function resolvePortals(context: SSRContext) {
}
}
}

async function renderSuspense(
vnode: VNode,
parentComponent: ComponentInternalInstance
): Promise<ResolvedSSRBuffer> {
const { content, fallback } = normalizeSuspenseChildren(vnode)
try {
const { push, getBuffer } = createBuffer()
renderVNode(push, content, parentComponent)
return await getBuffer()
} catch {
const { push, getBuffer } = createBuffer()
renderVNode(push, fallback, parentComponent)
return getBuffer()
}
}

0 comments on commit 4ab6ea6

Please sign in to comment.