Vue Server Side Render Plugin for Egg.
- Support Vue Server Side Render Mode And Vue Client Render Mode
- Support Asset Render Layout by Nunjucks Or ejs Enigne
- Support Vue Server Side Render Error, Auto Try Client Render
- Support Static Resouce Auto Inject to Html
$ npm i egg-view-vue-ssr --save
https://www.yuque.com/easy-team/egg-vue
// {app_root}/config/plugin.js
exports.vuessr = {
enable: true,
package: 'egg-view-vue-ssr',
};
// {app_root}/config/config.default.js
exports.vuessr = {
// layout: path.join(app.baseDir, 'app/view/layout.html'),
// manifest: path.join(app.baseDir, 'config/manifest.json'),
// injectCss: true,
// injectJs: true,
// fallbackToClient: true, // fallback to client rendering after server rendering failed
// afterRender: (html, ctx) => {
// return html;
// },
};
- doctype: {String} html content will auto add
<!doctype html>
, you can setdoctype: ''
- layout: {String} client render template, support renderString compile
- manifest: {Object} static resource dependence, the content such as:
{
"app/app.js": "/public/js/app/app.js",
"vendor.js": "/public/js/vendor.js",
"deps": {
"app/app.js": {
"js": [
"/public/js/vendor.js",
"/public/js/app/app.js"
],
"css": [
"/public/css/vendor.css",
"/public/css/app.css"
]
}
}
}
- injectCss: {Boolean} whether inject href css, default true
- injectJs: {Boolean} whether inject src script, default true
- injectRes: {Boolean} inline/inject css or js to file head or body. include location and src config inline {Boolean} true or false, default false location {String} headBefore, headAfter, bodyBefore, bodyAfter insert location, default headBefore url {String} inline file absolution path
- viewEngine egg-view render engine, only valid when renderAsset rendering,default nunjucks
- mergeLocals {Boolean} whether merge ctx locals to data, default true
- crossorigin: {Boolean} js cross domain support for cdn js error catch, default false
- fallbackToClient: {Boolean} fallback to client rendering if server render failed, default true
- cache: lru-cache options @see https://www.npmjs.com/package/lru-cache
- renderOptions: @see https://ssr.vuejs.org/en/api.html#renderer-options
- afterRender: afterRender hook html after render
when server render bundle error, will try client render**
// controller/home.js
exports.index = function* (ctx) {
yield ctx.render('index/index.js', { message: 'egg vue server side render'});
};
when server render bundle error, will try client render**
// controller/home.js
exports.index = function* (ctx) {
const html = yield ctx.renderToHtml('index/index.js', { message: 'egg vue server side render'});
// you can process html
ctx.body = html;
};
when client render, render layout
exports.vuessr.layout
by Vue
// controller/home.js
exports.client = function* (ctx) {
yield ctx.renderClient('index/index.js',{ message: 'egg vue client render'});
};
- when asset render, you can render layout
exports.vuessr.layout
by viewEngine, default useegg-view-nunjucks
- you must install the specified engine dependence, such as
egg-view-nunjucks
oregg-view-ejs
- The context provides an
asset
object that can getjs
,css
,state
information. layout template
// controller/home.js
exports.asset = function* (ctx) {
yield ctx.renderAsset('index/index.js', { message: 'egg vue asset render'});
};
// controller/home.js
exports.asset = function* (ctx) {
yield ctx.renderAsset('index/index.js', { message: 'egg vue asset render'}, { viewEngine: 'ejs' });
};
- see config/config.default.js for more detail.
- see egg-vue-asset-boilerplate examples
Please open an issue here.