Skip to content

Commit

Permalink
fix(ssr): properly handle errors in async component
Browse files Browse the repository at this point in the history
fix #6778
  • Loading branch information
yyx990803 committed Dec 12, 2017
1 parent 86e4d75 commit 8936b8d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/server/create-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ export function createRenderer ({
return false
}, cb)
try {
render(component, write, context, () => {
render(component, write, context, err => {
if (template) {
result = templateRenderer.renderSync(result, context)
}
cb(null, result)
if (err) {
cb(err)
} else {
cb(null, result)
}
})
} catch (e) {
cb(e)
Expand Down
2 changes: 1 addition & 1 deletion src/server/render-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class RenderContext {
write: (text: string, next: Function) => void;
renderNode: (node: VNode, isRoot: boolean, context: RenderContext) => void;
next: () => void;
done: () => void;
done: (err: ?Error) => void;

modules: Array<(node: VNode) => ?string>;
directives: Object;
Expand Down
11 changes: 4 additions & 7 deletions src/server/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,18 @@ function renderAsyncComponent (node, isRoot, context) {
if (resolvedNode) {
renderComponent(resolvedNode, isRoot, context)
} else {
reject()
// invalid component, but this does not throw on the client
// so render empty comment node
context.write(`<!---->`, context.next)
}
}

const reject = err => {
console.error(`[vue-server-renderer] error when rendering async component:\n`)
if (err) console.error(err.stack)
context.write(`<!--${node.text}-->`, context.next)
}

if (factory.resolved) {
resolve(factory.resolved)
return
}

const reject = context.done
let res
try {
res = factory(resolve, reject)
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 @@ -575,6 +575,25 @@ describe('SSR: renderToString', () => {
})
})

it('should catch async component error', done => {
Vue.config.silent = true
renderToString(new Vue({
template: '<test-async></test-async>',
components: {
testAsync: () => Promise.resolve({
render () {
throw new Error('foo')
}
})
}
}), (err, result) => {
Vue.config.silent = false
expect(err).toBeTruthy()
expect(result).toBeUndefined()
done()
})
})

it('everything together', done => {
renderVmWithOptions({
template: `
Expand Down

0 comments on commit 8936b8d

Please sign in to comment.