Skip to content

Commit

Permalink
feat: add ssr renderToHtml method to context
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Jan 9, 2019
1 parent 57a56f9 commit e23ff87
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ module.exports = {
renderAsset(name, locals, options) {
return this.renderVueAsset(name, locals, options);
},
renderVueAsset(name, locals, options) {
renderToHtml(name, locals, options = {}) {
return this.app.vue.renderView(this, name, locals, options);
},
renderVueAsset(name, locals, options = {}) {
return this.app.vue.renderAsset(this, name, locals, options).then(html => {
this.body = html;
});
Expand Down
11 changes: 11 additions & 0 deletions lib/engine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const Vue = require('vue');
Expand Down Expand Up @@ -167,6 +168,16 @@ class Engine {
// egg-view 自动合并 ctx, request, response, helper
return ctx.renderString(template, context, { viewEngine });
}


renderView(ctx, name, locals, options = {}) {
locals = this.normalizeLocals(ctx, locals, options, false);
const filepath = path.join(this.app.config.view.root[0], name);
const context = { state: locals };
return this.render(filepath, context, options).then(html => {
return this.resource.inject(html, name, context, options);
});
}
}

module.exports = Engine;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-view-vue-ssr",
"version": "3.2.1",
"version": "3.3.0",
"description": "vue server side render solution for egg",
"eggPlugin": {
"name": "vuessr"
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/view-vue-ssr-test/app/controller/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ exports.render = function* (ctx) {
yield ctx.render('test/test.js', { message: 'vue server side render!' });
};

exports.renderToHtml = function* (ctx) {
this.app.locals.title = 'app_locals_render_ssr';
this.locals.description = 'app_context_locals_render_ssr';
this.body = yield ctx.renderToHtml('test/test.js', { message: 'vue server side render!' });
};

exports.renderLocals = function* (ctx) {
this.app.locals.title = 'app_locals_render_ssr';
this.locals.description = 'app_context_locals_render_ssr';
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/apps/view-vue-ssr-test/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = app => {
this.body = 'hi, vue ssr';
});
app.get('/render', app.controller.view.render);
app.get('/renderToHtml', app.controller.view.renderToHtml);
app.get('/renderLocals', app.controller.view.renderLocals);
app.get('/renderString', app.controller.view.renderString);
app.get('/renderClient', app.controller.view.renderClient);
Expand Down
16 changes: 16 additions & 0 deletions test/view-vue-ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ describe('test/view-vue-ssr.test.js', () => {
});
});

it('should GET /renderToHtml', () => {
return request(app.callback())
.get('/renderToHtml')
.expect(200)
.expect(res => {
assert(res.text.indexOf('"csrf"') > -1);
assert(res.text.indexOf('data-server-rendered="true"') > -1);
assert(res.text.indexOf('</body></html>') > -1);
assert(res.text.indexOf('<title>app_locals_render_ssr</title>') > -1);
assert(res.text.indexOf('vue server side render!') > -1);
assert(res.text.indexOf('/public/css/test/test.css') > -1);
assert(res.text.indexOf('/public/js/vendor.js"') > -1);
assert(res.text.indexOf('/public/js/test/test.js"') > -1);
});
});

it('should GET /renderClient', () => {
return request(app.callback())
.get('/renderClient')
Expand Down

0 comments on commit e23ff87

Please sign in to comment.