From 020d87af80f424d5aa8bf0b4f2642b00f86cd7b6 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Thu, 14 Jan 2021 15:08:34 +0800 Subject: [PATCH 1/3] fix(server-renderer): respect the mixed render function in ssr --- packages/runtime-core/src/component.ts | 6 +-- .../server-renderer/__tests__/render.spec.ts | 40 +++++++++++++++++++ packages/server-renderer/src/render.ts | 7 ++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 406e8e0e4f6..b1b973c5bab 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -669,8 +669,8 @@ function finishComponentSetup( // template / render function normalization if (__NODE_JS__ && isSSR) { - if (Component.render) { - instance.render = Component.render as InternalRenderFunction + if (!instance.render) { + instance.render = (Component.render || NOOP) as InternalRenderFunction } } else if (!instance.render) { // could be set from setup() @@ -710,7 +710,7 @@ function finishComponentSetup( } // warn missing template/render - if (__DEV__ && !Component.render && instance.render === NOOP) { + if (__DEV__ && !Component.render && instance.render === NOOP && !isSSR) { /* istanbul ignore if */ if (!compile && Component.template) { warn( diff --git a/packages/server-renderer/__tests__/render.spec.ts b/packages/server-renderer/__tests__/render.spec.ts index a02bf365cb7..96c7678ec39 100644 --- a/packages/server-renderer/__tests__/render.spec.ts +++ b/packages/server-renderer/__tests__/render.spec.ts @@ -97,6 +97,46 @@ function testRender(type: string, render: typeof renderToString) { ).toBe(`
hello
`) }) + test('components using defineComponent with extends option', async () => { + expect( + await render( + createApp( + defineComponent({ + extends: { + data() { + return { msg: 'hello' } + }, + render(this: any) { + return h('div', this.msg) + } + } + }) + ) + ) + ).toBe(`
hello
`) + }) + + test('components using defineComponent with mixins option', async () => { + expect( + await render( + createApp( + defineComponent({ + mixins: [ + { + data() { + return { msg: 'hello' } + }, + render(this: any) { + return h('div', this.msg) + } + } + ] + }) + ) + ) + ).toBe(`
hello
`) + }) + test('optimized components', async () => { expect( await render( diff --git a/packages/server-renderer/src/render.ts b/packages/server-renderer/src/render.ts index 938a4f83a4f..cfe182d9963 100644 --- a/packages/server-renderer/src/render.ts +++ b/packages/server-renderer/src/render.ts @@ -22,7 +22,8 @@ import { isString, isVoidTag, ShapeFlags, - isArray + isArray, + NOOP } from '@vue/shared' import { ssrRenderAttrs } from './helpers/ssrRenderAttrs' import { ssrCompile } from './helpers/ssrCompile' @@ -116,7 +117,7 @@ function renderComponentSubTree( ) } else { if ( - !instance.render && + instance.render === NOOP && !instance.ssrRender && !comp.ssrRender && isString(comp.template) @@ -156,7 +157,7 @@ function renderComponentSubTree( instance.ctx ) setCurrentRenderingInstance(null) - } else if (instance.render) { + } else if (instance.render !== NOOP) { renderVNode( push, (instance.subTree = renderComponentRoot(instance)), From 60cf7fab4a625d192f067776f6344e06909d36c2 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Thu, 14 Jan 2021 15:41:17 +0800 Subject: [PATCH 2/3] fix: test --- packages/server-renderer/src/render.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server-renderer/src/render.ts b/packages/server-renderer/src/render.ts index cfe182d9963..257149f574b 100644 --- a/packages/server-renderer/src/render.ts +++ b/packages/server-renderer/src/render.ts @@ -157,7 +157,7 @@ function renderComponentSubTree( instance.ctx ) setCurrentRenderingInstance(null) - } else if (instance.render !== NOOP) { + } else if (instance.render && instance.render !== NOOP) { renderVNode( push, (instance.subTree = renderComponentRoot(instance)), From 8a3e7eed7646ef34d40aaa22bf81b70f01a7cf98 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Wed, 10 Feb 2021 20:47:43 +0800 Subject: [PATCH 3/3] chore: comments --- packages/runtime-core/src/component.ts | 11 ++++++++--- packages/server-renderer/src/render.ts | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index b1b973c5bab..b5f1a5b90de 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -669,9 +669,13 @@ function finishComponentSetup( // template / render function normalization if (__NODE_JS__ && isSSR) { - if (!instance.render) { - instance.render = (Component.render || NOOP) as InternalRenderFunction - } + // 1. the render function may already exist, returned by `setup` + // 2. otherwise try to use the `Component.render` + // 3. if the component doesn't have a render function, + // set `instance.render` to NOOP so that it can inherit the render function from mixins/extend + instance.render = (instance.render || + Component.render || + NOOP) as InternalRenderFunction } else if (!instance.render) { // could be set from setup() if (compile && Component.template && !Component.render) { @@ -710,6 +714,7 @@ function finishComponentSetup( } // warn missing template/render + // the runtime compilation of template in SSR is done by server-render if (__DEV__ && !Component.render && instance.render === NOOP && !isSSR) { /* istanbul ignore if */ if (!compile && Component.template) { diff --git a/packages/server-renderer/src/render.ts b/packages/server-renderer/src/render.ts index 257149f574b..fab664275c9 100644 --- a/packages/server-renderer/src/render.ts +++ b/packages/server-renderer/src/render.ts @@ -117,7 +117,7 @@ function renderComponentSubTree( ) } else { if ( - instance.render === NOOP && + (!instance.render || instance.render === NOOP) && !instance.ssrRender && !comp.ssrRender && isString(comp.template)