Skip to content

Commit 015e059

Browse files
committed
update to use vue 2.2.4
1 parent 6cd1ab4 commit 015e059

File tree

6 files changed

+599
-216
lines changed

6 files changed

+599
-216
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ HackerNews clone built with Vue 2.0 + vue-router + vuex, with server-side render
1818
- Vue + vue-router + vuex working together
1919
- Server-side data pre-fetching
2020
- Client-side state & DOM hydration
21+
- Automatically inlines CSS used by rendered components only
2122
- Single-file Vue Components
2223
- Hot-reload in development
2324
- CSS extraction for production

build/setup-dev-server.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const MFS = require('memory-fs')
44
const clientConfig = require('./webpack.client.config')
55
const serverConfig = require('./webpack.server.config')
66

7-
module.exports = function setupDevServer (app, opts) {
7+
module.exports = function setupDevServer (app, cb) {
8+
let bundle
9+
let template
10+
811
// modify client config to work with hot middleware
912
clientConfig.entry.app = ['webpack-hot-middleware/client', clientConfig.entry.app]
1013
clientConfig.output.filename = '[name].js'
@@ -27,8 +30,10 @@ module.exports = function setupDevServer (app, opts) {
2730
const fs = devMiddleware.fileSystem
2831
const filePath = path.join(clientConfig.output.path, 'index.html')
2932
if (fs.existsSync(filePath)) {
30-
const index = fs.readFileSync(filePath, 'utf-8')
31-
opts.templateUpdated(index)
33+
template = fs.readFileSync(filePath, 'utf-8')
34+
if (bundle) {
35+
cb(bundle, template)
36+
}
3237
}
3338
})
3439

@@ -47,6 +52,9 @@ module.exports = function setupDevServer (app, opts) {
4752

4853
// read bundle generated by vue-ssr-webpack-plugin
4954
const bundlePath = path.join(serverConfig.output.path, 'vue-ssr-bundle.json')
50-
opts.bundleUpdated(JSON.parse(mfs.readFileSync(bundlePath, 'utf-8')))
55+
bundle = JSON.parse(mfs.readFileSync(bundlePath, 'utf-8'))
56+
if (template) {
57+
cb(bundle, template)
58+
}
5159
})
5260
}

package.json

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,36 @@
1616
},
1717
"dependencies": {
1818
"compression": "^1.6.2",
19-
"cross-env": "^3.1.3",
20-
"es6-promise": "^4.0.5",
21-
"express": "^4.14.0",
22-
"firebase": "^3.6.2",
19+
"cross-env": "^3.2.4",
20+
"es6-promise": "^4.1.0",
21+
"express": "^4.15.2",
22+
"firebase": "^3.7.2",
2323
"lru-cache": "^4.0.2",
2424
"serialize-javascript": "^1.3.0",
25-
"serve-favicon": "^2.3.2",
26-
"vue": "^2.1.10",
27-
"vue-router": "^2.1.0",
28-
"vue-server-renderer": "^2.1.10",
29-
"vue-ssr-html-stream": "^1.0.0",
30-
"vuex": "^2.1.0",
31-
"vuex-router-sync": "^4.0.2"
25+
"serve-favicon": "^2.4.1",
26+
"vue": "^2.2.4",
27+
"vue-router": "^2.3.0",
28+
"vue-server-renderer": "^2.2.4",
29+
"vuex": "^2.2.1",
30+
"vuex-router-sync": "^4.1.2"
3231
},
3332
"devDependencies": {
34-
"autoprefixer": "^6.5.3",
35-
"buble": "^0.15.1",
36-
"buble-loader": "^0.4.0",
37-
"css-loader": "^0.26.0",
38-
"file-loader": "^0.9.0",
39-
"html-webpack-plugin": "^2.24.1",
40-
"rimraf": "^2.5.4",
33+
"autoprefixer": "^6.7.7",
34+
"buble": "^0.15.2",
35+
"buble-loader": "^0.4.1",
36+
"css-loader": "^0.27.3",
37+
"file-loader": "^0.10.1",
38+
"html-webpack-plugin": "^2.28.0",
39+
"rimraf": "^2.6.1",
4140
"stylus": "^0.54.5",
42-
"stylus-loader": "^2.4.0",
43-
"sw-precache-webpack-plugin": "^0.7.0",
44-
"url-loader": "^0.5.7",
45-
"vue-loader": "^10.2.0",
46-
"vue-ssr-webpack-plugin": "^1.0.0",
47-
"vue-template-compiler": "^2.1.8",
48-
"webpack": "^2.2.0",
49-
"webpack-dev-middleware": "^1.8.4",
50-
"webpack-hot-middleware": "^2.13.2"
41+
"stylus-loader": "^3.0.1",
42+
"sw-precache-webpack-plugin": "^0.9.1",
43+
"url-loader": "^0.5.8",
44+
"vue-loader": "^11.1.4",
45+
"vue-ssr-webpack-plugin": "^1.0.2",
46+
"vue-template-compiler": "^2.2.4",
47+
"webpack": "^2.2.1",
48+
"webpack-dev-middleware": "^1.10.1",
49+
"webpack-hot-middleware": "^2.17.1"
5150
}
5251
}

server.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const path = require('path')
33
const express = require('express')
44
const favicon = require('serve-favicon')
55
const compression = require('compression')
6-
const HTMLStream = require('vue-ssr-html-stream')
76
const resolve = file => path.resolve(__dirname, file)
87

98
const isProd = process.env.NODE_ENV === 'production'
@@ -13,28 +12,28 @@ const serverInfo =
1312

1413
const app = express()
1514

16-
let template // generated by html-webpack-plugin
17-
let renderer // created from the webpack-generated server bundle
15+
let renderer
1816
if (isProd) {
19-
// in production: create server renderer and index HTML from real fs
20-
renderer = createRenderer(require('./dist/vue-ssr-bundle.json'))
21-
template = fs.readFileSync(resolve('./dist/index.html'), 'utf-8')
17+
// In production: create server renderer using server bundle and index HTML
18+
// template from real fs.
19+
// The server bundle is generated by vue-ssr-webpack-plugin.
20+
const bundle = require('./dist/vue-ssr-bundle.json')
21+
// src/index.template.html is processed by html-webpack-plugin to inject
22+
// build assets and output as dist/index.html.
23+
const template = fs.readFileSync(resolve('./dist/index.html'), 'utf-8')
24+
renderer = createRenderer(bundle, template)
2225
} else {
23-
// in development: setup the dev server with watch and hot-reload,
24-
// and update renderer / index HTML on file change.
25-
require('./build/setup-dev-server')(app, {
26-
bundleUpdated: bundle => {
27-
renderer = createRenderer(bundle)
28-
},
29-
templateUpdated: _template => {
30-
template = _template
31-
}
26+
// In development: setup the dev server with watch and hot-reload,
27+
// and create a new renderer on bundle / index template update.
28+
require('./build/setup-dev-server')(app, (bundle, template) => {
29+
renderer = createRenderer(bundle, template)
3230
})
3331
}
3432

35-
function createRenderer (bundle) {
33+
function createRenderer (bundle, template) {
3634
// https://github.com/vuejs/vue/blob/dev/packages/vue-server-renderer/README.md#why-use-bundlerenderer
3735
return require('vue-server-renderer').createBundleRenderer(bundle, {
36+
template,
3837
cache: require('lru-cache')({
3938
max: 1000,
4039
maxAge: 1000 * 60 * 15
@@ -68,18 +67,14 @@ app.get('*', (req, res) => {
6867
res.status(404).end('404 | Page Not Found')
6968
} else {
7069
// Render Error Page or Redirect
71-
res.status(500).end('Internal Error 500')
70+
res.status(500).end('500 | Internal Server Error')
7271
console.error(`error during render : ${req.url}`)
7372
console.error(err)
7473
}
7574
}
7675

77-
const context = { url: req.url }
78-
const htmlStream = new HTMLStream({ template, context })
79-
80-
renderer.renderToStream(context)
76+
renderer.renderToStream({ url: req.url })
8177
.on('error', errorHandler)
82-
.pipe(htmlStream)
8378
.on('end', () => console.log(`whole request: ${Date.now() - s}ms`))
8479
.pipe(res)
8580
})

src/index.template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<% } %>
1818
</head>
1919
<body>
20-
<!-- APP -->
20+
<!--vue-ssr-outlet-->
2121
</body>
2222
</html>

0 commit comments

Comments
 (0)