Skip to content

Commit

Permalink
feat(ssr): support getSSRProps for vnode directives
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 16, 2020
1 parent a46f3b3 commit c450ede
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 6 deletions.
10 changes: 8 additions & 2 deletions packages/runtime-core/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ return withDirectives(h(comp), [
import { VNode } from './vnode'
import { isFunction, EMPTY_OBJ, makeMap, EMPTY_ARR } from '@vue/shared'
import { warn } from './warning'
import { ComponentInternalInstance } from './component'
import { ComponentInternalInstance, Data } from './component'
import { currentRenderingInstance } from './componentRenderUtils'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { ComponentPublicInstance } from './componentProxy'
Expand All @@ -35,13 +35,19 @@ export type DirectiveHook<T = any> = (
prevVNode: VNode<any, T> | null
) => void

export type SSRDirectiveHook = (
binding: DirectiveBinding,
vnode: VNode
) => Data | undefined

export interface ObjectDirective<T = any> {
beforeMount?: DirectiveHook<T>
mounted?: DirectiveHook<T>
beforeUpdate?: DirectiveHook<T>
updated?: DirectiveHook<T>
beforeUnmount?: DirectiveHook<T>
unmounted?: DirectiveHook<T>
getSSRProps?: SSRDirectiveHook
}

export type FunctionDirective<T = any> = DirectiveHook<T>
Expand Down Expand Up @@ -81,7 +87,7 @@ const directiveToVnodeHooksMap = /*#__PURE__*/ [
const prevBindings = prevVnode ? prevVnode.dirs! : EMPTY_ARR
for (let i = 0; i < bindings.length; i++) {
const binding = bindings[i]
const hook = binding.dir[key]
const hook = binding.dir[key] as DirectiveHook
if (hook != null) {
if (prevVnode != null) {
binding.oldValue = prevBindings[i].value
Expand Down
23 changes: 22 additions & 1 deletion packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function callModelHook(
binding: DirectiveBinding,
vnode: VNode,
prevVNode: VNode | null,
hook: keyof ObjectDirective
hook: 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated'
) {
let modelToUse: ObjectDirective
switch (el.tagName) {
Expand All @@ -243,3 +243,24 @@ function callModelHook(
const fn = modelToUse[hook]
fn && fn(el, binding, vnode, prevVNode)
}

// SSR vnode transforms
if (__NODE_JS__) {
vModelText.getSSRProps = ({ value }) => ({ value })

vModelRadio.getSSRProps = ({ value }, vnode) => {
if (vnode.props && looseEqual(vnode.props.value, value)) {
return { checked: true }
}
}

vModelCheckbox.getSSRProps = ({ value }, vnode) => {
if (isArray(value)) {
if (vnode.props && looseIndexOf(value, vnode.props.value) > -1) {
return { checked: true }
}
} else if (value) {
return { checked: true }
}
}
}
8 changes: 8 additions & 0 deletions packages/runtime-dom/src/directives/vShow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export const vShow: ObjectDirective<VShowElement> = {
}
}

if (__NODE_JS__) {
vShow.getSSRProps = ({ value }) => {
if (!value) {
return { style: { display: 'none' } }
}
}
}

function setDisplay(el: VShowElement, value: unknown): void {
el.style.display = value ? el._vod : 'none'
}
Loading

0 comments on commit c450ede

Please sign in to comment.