-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathserver.js
executable file
·127 lines (105 loc) · 3.38 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
process.env.VUE_ENV = 'server'
const isProd = process.env.NODE_ENV === 'production'
const compression = require('compression')
const fs = require('fs')
const express = require('express')
const favicon = require('serve-favicon')
const path = require('path')
const resolve = file => path.resolve(__dirname, file)
const serialize = require('serialize-javascript')
const app = express()
let indexHTML // generated by html-webpack-plugin
let renderer // created from the webpack-generated server bundle
if (isProd) {
// in production: create server renderer and index HTML from real fs
renderer = createRenderer(fs.readFileSync(resolve('./dist/server-bundle.js'), 'utf-8'))
indexHTML = parseIndex(fs.readFileSync(resolve('./dist/index.html'), 'utf-8'))
} else {
require('./build/setup-dev-server')(app, {
bundleUpdated: bundle => {
renderer = createRenderer(bundle)
},
indexUpdated: index => {
indexHTML = parseIndex(index)
}
})
}
function createRenderer (bundle) {
// https://github.com/vuejs/vue/blob/next/packages/vue-server-renderer/README.md#why-use-bundlerenderer
return require('vue-server-renderer').createBundleRenderer(bundle, {
cache: require('lru-cache')({
max: 1000,
maxAge: 1000 * 60 * 15
})
})
}
function parseIndex (template) {
const appMarker = '<!-- APP -->'
const i = template.indexOf(appMarker)
return {
head: template.slice(0, i),
tail: template.slice(i + appMarker.length)
}
}
function updateMeta (head, context) {
const title = context.title || false
const description = context.description || false
const keywords = context.keywords || false
if (title) {
head = head.replace(/(<title>)(.*?)(<\/title>)/, `$1${title}$3`)
}
if (description) {
head = head.replace(/(<meta name="description" content=")(.*?)(">)/, `$1${description}$3`)
}
if (keywords) {
head = head.replace(/(<meta name="keywords" content=")(.*?)(">)/, `$1${keywords}$3`)
}
return head
}
const serve = (path, cache) => express.static(resolve(path), {
maxAge: cache && isProd ? 60 * 60 * 24 * 30 : 0
})
app.use(compression({ threshold: 0 }))
app.use(favicon('./public/favicon-32x32.png'))
app.use('/manifest.json', serve('./manifest.json'))
app.use('/dist', serve('./dist'))
app.use('/public', serve('./public'))
app.get('*', (req, res) => {
if (!renderer) {
return res.end('waiting for compilation... refresh in a moment.')
}
res.setHeader("Content-Type", "text/html");
const context = { url: req.url }
const renderStream = renderer.renderToStream(context)
renderStream.once('data', () => {
res.write(updateMeta(indexHTML.head, context))
})
renderStream.on('data', chunk => {
res.write(chunk)
})
renderStream.on('end', () => {
// embed initial store state
if (context.initialState) {
res.write(
`<script>window.__INITIAL_STATE__=${
serialize(context.initialState, { isJSON: true })
}</script>`
)
}
res.end(indexHTML.tail)
})
renderStream.on('error', err => {
if (err && err.code === '404') {
res.status(404).end('404 | Page Not Found')
return
}
// Render Error Page or Redirect
res.status(500).end('Internal Error 500')
console.error(`error during render : ${req.url}`)
console.error(err)
})
})
const port = process.env.PORT || 8080
app.listen(port, () => {
console.log(`server started at localhost:${port}`)
})