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

feat(server-renderer): compile templates on-the-fly #707

Merged
merged 7 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
76 changes: 74 additions & 2 deletions packages/server-renderer/__tests__/renderToString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ describe('ssr: renderToString', () => {
).toBe(`<div>hello</div>`)
})

test('template components', async () => {
expect(
await renderToString(
createApp({
data() {
return { msg: 'hello' }
},
template: `<div>{{ msg }}</div>`
})
)
).toBe(`<div>hello</div>`)
})

test('nested vnode components', async () => {
const Child = {
props: ['msg'],
Expand Down Expand Up @@ -96,7 +109,22 @@ describe('ssr: renderToString', () => {
).toBe(`<div>parent<div>hello</div></div>`)
})

test('mixing optimized / vnode components', async () => {
test('nested template components', async () => {
const Child = {
props: ['msg'],
template: `<div>{{ msg }}</div>`
}
const app = createApp({
template: `<div>parent<Child msg="hello" /></div>`
})
app.component('Child', Child)

expect(await renderToString(app)).toBe(
`<div>parent<div>hello</div></div>`
)
})

test('mixing optimized / vnode / template components', async () => {
const OptimizedChild = {
props: ['msg'],
ssrRender(ctx: any, push: any) {
Expand All @@ -111,6 +139,11 @@ describe('ssr: renderToString', () => {
}
}

const TemplateChild = {
props: ['msg'],
template: `<div>{{ msg }}</div>`
}

expect(
await renderToString(
createApp({
Expand All @@ -120,11 +153,21 @@ describe('ssr: renderToString', () => {
renderComponent(OptimizedChild, { msg: 'opt' }, null, parent)
)
push(renderComponent(VNodeChild, { msg: 'vnode' }, null, parent))
push(
renderComponent(
TemplateChild,
{ msg: 'template' },
null,
parent
)
)
push(`</div>`)
}
})
)
).toBe(`<div>parent<div>opt</div><div>vnode</div></div>`)
).toBe(
`<div>parent<div>opt</div><div>vnode</div><div>template</div></div>`
)
})

test('nested components with optimized slots', async () => {
Expand Down Expand Up @@ -236,6 +279,35 @@ describe('ssr: renderToString', () => {
)
})

test('nested components with template slots', async () => {
const Child = {
props: ['msg'],
ssrRender(ctx: any, push: any, parent: any) {
dsseng marked this conversation as resolved.
Show resolved Hide resolved
push(`<div class="child">`)
ssrRenderSlot(
ctx.$slots,
'default',
{ msg: 'from slot' },
null,
push,
parent
)
push(`</div>`)
}
}

const app = createApp({
template: `<div>parent<Child v-slot="{ msg }"><span>{{ msg }}</span></Child></div>`
})
app.component('Child', Child)

expect(await renderToString(app)).toBe(
`<div>parent<div class="child">` +
`<!----><span>from slot</span><!---->` +
`</div></div>`
)
})

test('async components', async () => {
const Child = {
// should wait for resovled render context from setup()
Expand Down
3 changes: 3 additions & 0 deletions packages/server-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/server-renderer#readme",
"peerDependencies": {
"vue": "3.0.0-alpha.4"
},
"dependencies": {
"@vue/compiler-ssr": "3.0.0-alpha.4"
}
}
17 changes: 14 additions & 3 deletions packages/server-renderer/src/renderToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import {
isArray,
isFunction,
isVoidTag,
escapeHtml
escapeHtml,
NO
} from '@vue/shared'
import { compile } from '@vue/compiler-ssr'
import { ssrRenderAttrs } from './helpers/ssrRenderAttrs'
import { SSRSlots } from './helpers/ssrRenderSlot'

Expand Down Expand Up @@ -142,12 +144,21 @@ function renderComponentSubTree(
setCurrentRenderingInstance(null)
} else if (comp.render) {
renderVNode(push, renderComponentRoot(instance), instance)
} else if (comp.template && isString(comp.template)) {
dsseng marked this conversation as resolved.
Show resolved Hide resolved
const compileResult = compile(comp.template, {
isCustomElement: instance.appContext.config.isCustomElement || NO
dsseng marked this conversation as resolved.
Show resolved Hide resolved
})
const ssrRender = Function(compileResult.code)()

// set current rendering instance for asset resolution
setCurrentRenderingInstance(instance)
ssrRender(instance.proxy, push, instance)
setCurrentRenderingInstance(null)
} else {
// TODO on the fly template compilation support
throw new Error(
`Component ${
comp.name ? `${comp.name} ` : ``
} is missing render function.`
} doesn't provide template or render function.`
dsseng marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
Expand Down