Skip to content

Commit

Permalink
fix: install ssr helpers for functional context during SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 8, 2018
1 parent 3d431a5 commit 9b22d86
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
2 changes: 2 additions & 0 deletions flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ declare interface Component {
static directive: (id: string, def?: Function | Object) => Function | Object | void;
static component: (id: string, def?: Class<Component> | Object) => Class<Component>;
static filter: (id: string, def?: Function) => Function | void;
// functional context constructor
static FunctionalRenderContext: Function;

// public properties
$el: any; // so that we can attach __vue__ to it
Expand Down
2 changes: 1 addition & 1 deletion scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const builds = {
format: 'cjs',
external: Object.keys(require('../packages/vue-server-renderer/package.json').dependencies)
},
'web-server-basic-renderer': {
'web-server-renderer-basic': {
entry: resolve('web/entry-server-basic-renderer.js'),
dest: resolve('packages/vue-server-renderer/basic.js'),
format: 'umd',
Expand Down
6 changes: 6 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from './instance/index'
import { initGlobalAPI } from './global-api/index'
import { isServerRendering } from 'core/util/env'
import { FunctionalRenderContext } from 'core/vdom/create-functional-component'

initGlobalAPI(Vue)

Expand All @@ -15,6 +16,11 @@ Object.defineProperty(Vue.prototype, '$ssrContext', {
}
})

// expose FunctionalRenderContext for ssr runtime helper installation
Object.defineProperty(Vue, 'FunctionalRenderContext', {
value: FunctionalRenderContext
})

Vue.version = '__VERSION__'

export default Vue
12 changes: 6 additions & 6 deletions src/core/vdom/create-functional-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
validateProp
} from '../util/index'

function FunctionalRenderContext (
data,
props,
children,
parent,
Ctor
export function FunctionalRenderContext (
data: VNodeData,
props: Object,
children: ?Array<VNode>,
parent: Component,
Ctor: Class<Component>
) {
const options = Ctor.options
this.data = data
Expand Down
35 changes: 21 additions & 14 deletions src/server/optimizing-compiler/runtime-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ import {
isRenderableAttr
} from 'web/server/util'

const ssrHelpers = {
_ssrEscape: escape,
_ssrNode: renderStringNode,
_ssrList: renderStringList,
_ssrAttr: renderAttr,
_ssrAttrs: renderAttrs,
_ssrDOMProps: renderDOMProps,
_ssrClass: renderSSRClass,
_ssrStyle: renderSSRStyle
}

export function installSSRHelpers (vm: Component) {
if (vm._ssrNode) return
let Ctor = vm.constructor
while (Ctor.super) {
Ctor = Ctor.super
if (vm._ssrNode) {
return
}
let Vue = vm.constructor
while (Vue.super) {
Vue = Vue.super
}
extend(Vue.prototype, ssrHelpers)
if (Vue.FunctionalRenderContext) {
extend(Vue.FunctionalRenderContext.prototype, ssrHelpers)
}
extend(Ctor.prototype, {
_ssrEscape: escape,
_ssrNode: renderStringNode,
_ssrList: renderStringList,
_ssrAttr: renderAttr,
_ssrAttrs: renderAttrs,
_ssrDOMProps: renderDOMProps,
_ssrClass: renderSSRClass,
_ssrStyle: renderSSRStyle
})
}

class StringNode {
Expand Down
19 changes: 19 additions & 0 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,25 @@ describe('SSR: renderToString', () => {
done()
})
})

it('should expose ssr helpers on functional context', done => {
let called = false
renderVmWithOptions({
template: `<div><foo/></div>`,
components: {
foo: {
functional: true,
render (h, ctx) {
expect(ctx._ssrNode).toBeTruthy()
called = true
}
}
}
}, () => {
expect(called).toBe(true)
done()
})
})
})

function renderVmWithOptions (options, cb) {
Expand Down

0 comments on commit 9b22d86

Please sign in to comment.