Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染3配置webpack支持ssr
安装 cross-env
yarn add -D cross-env
安装 html-webpack-plugin
yarn add -D html-webpack-plugin
安装 webpack-merge
yarn add -D webpack-merge
安装 vue-server-renderer
yarn add -D vue-server-renderer
测试
server.js
const renderVueComponentToString = require("vue-server-renderer/basic.js");
const serverBundle = require("./dist/server-bundle");
console.log("server is running ...");
const context = {
url: "/"
};
const promise = serverBundle.default(context);
promise
.then((resolve, reject) => {
console.log("promise resolved");
const vm = resolve;
renderVueComponentToString(vm, (err, res) => {
if (err) {
console.log(err);
return;
}
console.log(res);
});
})
.catch(rejected => {
console.log(rejected);
});
运行结果
➜ webapp git:(j2v8-version) ✗ yarn dev
yarn run v1.13.0
$ node server
server is running ...
cntext=> { url: '/' }
promise resolved
<div data-server-rendered="true"><h1>Hello World! Vue,j2v8!</h1></div>
Done in 0.69s.