├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── server └── index.js ├── src ├── App.vue ├── MyCmp.vue ├── client.js └── server.js ├── ssr-bundle.js ├── ssr.js └── webpack ├── webpack.client.js └── webpack.server.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ "es2015" ] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | dist 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Hans Chan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue2-ssr-example 2 | 3 | Vue 2.0 Server-Side Rendering Example. [Read more](http://csbun.github.io/blog/2016/08/vue-2-0-server-side-rendering/). 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install 9 | ``` 10 | 11 | ## Development 12 | 13 | ```bash 14 | npm start 15 | ``` 16 | 17 | Hit [http://127.0.0.1:3000/](http://127.0.0.1:3000/) on your favorite browser. 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-ssr", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "watch-c": "webpack --config webpack/webpack.client.js -w", 8 | "watch-s": "webpack --config webpack/webpack.server.js -w", 9 | "build-c": "webpack --config webpack/webpack.client.js", 10 | "build-s": "webpack --config webpack/webpack.server.js", 11 | "build": "npm run build-c && npm run build-s", 12 | "start": "npm run build && node server/index.js" 13 | }, 14 | "dependencies": { 15 | "express": "^4.14.0", 16 | "vue": "^2.0.0-beta.6", 17 | "vue-server-renderer": "^2.0.0-beta.6" 18 | }, 19 | "devDependencies": { 20 | "babel-core": "^6.11.4", 21 | "babel-loader": "^6.2.4", 22 | "babel-preset-es2015": "^6.9.0", 23 | "css-loader": "^0.23.1", 24 | "vue-loader": "^9.3.1", 25 | "webpack": "^1.13.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /server/index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const express = require('express'); 4 | const vueServerRenderer = require('vue-server-renderer'); 5 | 6 | const app = express(); 7 | 8 | // Server-Side Bundle File 9 | const serverBundleFilePath = path.join(__dirname, '../dist/bundle.server.js') 10 | const serverBundleFileCode = fs.readFileSync(serverBundleFilePath, 'utf8'); 11 | const bundleRenderer = vueServerRenderer.createBundleRenderer(serverBundleFileCode); 12 | 13 | // Client-Side Bundle File 14 | const clientBundleFilePath = path.join(__dirname, '../dist/bundle.client.js'); 15 | const clientBundleFileUrl = '/bundle.client.js'; 16 | 17 | // Server-Side Rendering 18 | app.get('/', function (req, res) { 19 | bundleRenderer.renderToString((err, html) => { 20 | if (err){ 21 | res.status(500).send(` 22 |
${err.stack}24 | `); 25 | } else { 26 | res.send(` 27 | 28 | 29 | 30 | 31 |
Hello {{ name }}
4 |Welcome to China!
5 |this is a component
`, 6 | }); 7 | 8 | const App = Vue.component('my-app', { 9 | components: { 10 | Cmp, 11 | }, 12 | template: ` 13 |