Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server-renderer): respect the mixed render function in ssr #3006

Merged
merged 3 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,13 @@ function finishComponentSetup(

// template / render function normalization
if (__NODE_JS__ && isSSR) {
if (Component.render) {
instance.render = Component.render 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) {
Expand Down Expand Up @@ -710,7 +714,8 @@ function finishComponentSetup(
}

// warn missing template/render
if (__DEV__ && !Component.render && instance.render === NOOP) {
// 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) {
warn(
Expand Down
40 changes: 40 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,46 @@ function testRender(type: string, render: typeof renderToString) {
).toBe(`<div>hello</div>`)
})

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(`<div>hello</div>`)
})

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(`<div>hello</div>`)
})

test('optimized components', async () => {
expect(
await render(
Expand Down
7 changes: 4 additions & 3 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
isString,
isVoidTag,
ShapeFlags,
isArray
isArray,
NOOP
} from '@vue/shared'
import { ssrRenderAttrs } from './helpers/ssrRenderAttrs'
import { ssrCompile } from './helpers/ssrCompile'
Expand Down Expand Up @@ -116,7 +117,7 @@ function renderComponentSubTree(
)
} else {
if (
!instance.render &&
(!instance.render || instance.render === NOOP) &&
!instance.ssrRender &&
!comp.ssrRender &&
isString(comp.template)
Expand Down Expand Up @@ -156,7 +157,7 @@ function renderComponentSubTree(
instance.ctx
)
setCurrentRenderingInstance(null)
} else if (instance.render) {
} else if (instance.render && instance.render !== NOOP) {
renderVNode(
push,
(instance.subTree = renderComponentRoot(instance)),
Expand Down