Skip to content

Commit dff85b2

Browse files
committedOct 10, 2017
fix(ssr): handle inline template compilation error
fix #6766
1 parent 70a28b3 commit dff85b2

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed
 

‎src/compiler/to-function.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

3-
import { noop } from 'shared/util'
4-
import { warn, tip } from 'core/util/debug'
3+
import { noop, extend } from 'shared/util'
4+
import { warn as baseWarn, tip } from 'core/util/debug'
55

66
type CompiledFunctionResult = {
77
render: Function;
@@ -27,7 +27,9 @@ export function createCompileToFunctionFn (compile: Function): Function {
2727
options?: CompilerOptions,
2828
vm?: Component
2929
): CompiledFunctionResult {
30-
options = options || {}
30+
options = extend({}, options)
31+
const warn = options.warn || baseWarn
32+
delete options.warn
3133

3234
/* istanbul ignore if */
3335
if (process.env.NODE_ENV !== 'production') {

‎src/core/util/debug.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { noop } from 'shared/util'
55

66
export let warn = noop
77
export let tip = noop
8-
export let formatComponentName: Function = (null: any) // work around flow check
8+
export let generateComponentTrace = (noop: any) // work around flow check
9+
export let formatComponentName = (noop: any)
910

1011
if (process.env.NODE_ENV !== 'production') {
1112
const hasConsole = typeof console !== 'undefined'
@@ -66,7 +67,7 @@ if (process.env.NODE_ENV !== 'production') {
6667
return res
6768
}
6869

69-
const generateComponentTrace = vm => {
70+
generateComponentTrace = vm => {
7071
if (vm._isVue && vm.$parent) {
7172
const tree = []
7273
let currentRecursiveSequence = 0

‎src/server/render.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
/* @flow */
22

3-
import {
4-
isDef,
5-
isUndef,
6-
isTrue,
7-
extend
8-
} from 'shared/util'
9-
103
import { escape } from 'web/server/util'
114
import { SSR_ATTR } from 'shared/constants'
125
import { RenderContext } from './render-context'
6+
import { generateComponentTrace } from 'core/util/debug'
137
import { ssrCompileToFunctions } from 'web/server/compiler'
148
import { installSSRHelpers } from './optimizing-compiler/runtime-helpers'
159

10+
import { isDef, isUndef, isTrue } from 'shared/util'
11+
1612
import {
1713
createComponent,
1814
createComponentInstanceForVnode
@@ -26,13 +22,22 @@ const warnOnce = msg => {
2622
}
2723
}
2824

25+
const onCompilationError = (err, vm) => {
26+
const trace = vm ? generateComponentTrace(vm) : ''
27+
throw new Error(`\n\u001b[31m${err}${trace}\u001b[39m\n`)
28+
}
29+
2930
const normalizeRender = vm => {
3031
const { render, template, _scopeId } = vm.$options
3132
if (isUndef(render)) {
3233
if (template) {
33-
extend(vm.$options, ssrCompileToFunctions(template, {
34-
scopeId: _scopeId
35-
}))
34+
const compiled = ssrCompileToFunctions(template, {
35+
scopeId: _scopeId,
36+
warn: onCompilationError
37+
}, vm)
38+
39+
vm.$options.render = compiled.render
40+
vm.$options.staticRenderFns = compiled.staticRenderFns
3641
} else {
3742
throw new Error(
3843
`render function or template not defined in component: ${

‎test/ssr/ssr-string.spec.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ describe('SSR: renderToString', () => {
968968
})
969969
})
970970

971-
it('should Promise (error)', done => {
971+
it('return Promise (error)', done => {
972972
Vue.config.silent = true
973973
renderToString(new Vue({
974974
render () {
@@ -980,6 +980,15 @@ describe('SSR: renderToString', () => {
980980
done()
981981
})
982982
})
983+
984+
it('should catch template compilation error', done => {
985+
renderToString(new Vue({
986+
template: `<div></div><div></div>`
987+
}), (err, res) => {
988+
expect(err.toString()).toContain('Component template should contain exactly one root element')
989+
done()
990+
})
991+
})
983992
})
984993

985994
function renderVmWithOptions (options, cb) {

0 commit comments

Comments
 (0)
Please sign in to comment.