Skip to content

Commit

Permalink
feat: add switch for server render error fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
yihang.lyh committed Apr 26, 2017
1 parent 64c4d88 commit 10791b2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ run/
.DS_Store
*.swp
*.iml
*.lock
.vscode
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports.vuessr = {
// buildConfig: path.join(app.baseDir, 'config/buildConfig.json'),
// injectCss: true,
// injectJs: true,
// fallbackToClient: true, // fallback to client rendering after server rendering failed
// afterRender: (html, context) => {
// return html;
// },
Expand All @@ -63,3 +64,5 @@ Please open an issue [here](https://github.com/hubcarl/egg-view-vue-ssr/issues).
## License

[MIT](LICENSE)


7 changes: 5 additions & 2 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ module.exports = {
const template = options.renderOptions && options.renderOptions.template || this.app.vue.resource.template;
const context = { state: locals };
const html = yield this.app.vue.renderCode(code, context, options).catch(err => {
this.app.logger.error('[%s] server render bundle error, try client render, the server render error', name, err);
return this.renderString(template, context.state);
if (config.fallbackToClient) {
this.app.logger.error('[%s] server render bundle error, try client render, the server render error', name, err);
return this.renderString(template, context.state);
}
throw err;
});
this.body = this.app.vue.resource.inject(name, html, context, config);
},
Expand Down
1 change: 1 addition & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = app => {
buildConfig: path.join(app.baseDir, 'config/buildConfig.json'),
injectCss: true,
injectJs: true,
fallbackToClient: true, // fallback to client rendering if server render failed
afterRender: (html, context) => { /* eslint no-unused-vars:off */
return html;
},
Expand Down
8 changes: 8 additions & 0 deletions test/view-vue-ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ describe('test/view-vue-ssr.test.js', () => {
assert(res.text.indexOf('/public/static/js/error/error.js"') > -1);
});
});

it('should GET /render server error, don\'t fallback', () => {
mm(app.config.vuessr, 'fallbackToClient', false);

return request(app.callback())
.get('/renderServerError')
.expect(500);
});
});

});

0 comments on commit 10791b2

Please sign in to comment.