forked from peluprvi/vuetifyjs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·163 lines (144 loc) · 5.53 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const fs = require('fs')
const path = require('path')
const LRU = require('lru-cache')
const express = require('express')
const cookieParser = require('cookie-parser')
const favicon = require('serve-favicon')
const compression = require('compression')
const microcache = require('route-cache')
const resolve = file => path.resolve(__dirname, file)
const { createBundleRenderer } = require('vue-server-renderer')
const redirects = require('./router/301.json')
const isProd = process.env.NODE_ENV === 'production'
const useMicroCache = process.env.MICRO_CACHE !== 'false'
const serverInfo =
`express/${require('express/package.json').version} ` +
`vue-server-renderer/${require('vue-server-renderer/package.json').version}`
const availableLanguages = require('./i18n/languages').map(lang => lang.locale)
const app = express()
// const template = fs.readFileSync(resolve('./assets/index.template.html'), 'utf-8')
function createRenderer (bundle, options) {
// https://github.com/vuejs/vue/blob/dev/packages/vue-server-renderer/README.md#why-use-bundlerenderer
return createBundleRenderer(bundle, Object.assign(options, {
// for component caching
cache: LRU({
max: 1000,
maxAge: 1000 * 60 * 15
}),
// this is only needed when vue-server-renderer is npm-linked
basedir: resolve('./public'),
// recommended for performance
runInNewContext: false
}))
}
let renderer
let readyPromise
const templatePath = resolve('./assets/index.template.html')
if (isProd) {
// In production: create server renderer using template and built server bundle.
// The server bundle is generated by vue-ssr-webpack-plugin.
const template = fs.readFileSync(templatePath, 'utf-8')
const bundle = require('./public/vue-ssr-server-bundle.json')
// The client manifests are optional, but it allows the renderer
// to automatically infer preload/prefetch links and directly add <script>
// tags for any async chunks used during render, avoiding waterfall requests.
const clientManifest = require('./public/vue-ssr-client-manifest.json')
renderer = createRenderer(bundle, {
template,
clientManifest
})
} else {
// In development: setup the dev server with watch and hot-reload,
// and create a new renderer on bundle / index template update.
readyPromise = require('./build/setup-dev-server')(
app,
templatePath,
(bundle, options) => {
renderer = createRenderer(bundle, options)
}
)
}
const serve = (path, cache) => express.static(resolve(path), {
maxAge: cache && isProd ? 1000 * 60 * 60 * 24 * 30 : 0
})
app.use(cookieParser())
app.use(compression({ threshold: 0 }))
app.use(favicon('./static/favicon.ico'))
app.use('/static/manifest.json', serve('./manifest.json', true))
app.use('/static', serve('./static', true))
app.use('/public', serve('./public', true))
app.use('/static/robots.txt', serve('./robots.txt'))
app.use('/releases', serve('./releases'))
app.use('/themes', serve('./themes'))
app.get('/releases/:release', (req, res) => {
res.setHeader('Content-Type', 'text/html')
res.sendFile(resolve(`./releases/${req.params.release}`))
})
app.get('/sitemap.xml', (req, res) => {
res.setHeader('Content-Type', 'text/xml')
res.sendFile(resolve('./static/sitemap.xml'))
})
// 301 redirect for changed routes
Object.keys(redirects).forEach(k => {
app.get(k, (req, res) => res.redirect(301, redirects[k]))
})
// since this app has no user-specific content, every page is micro-cacheable.
// if your app involves user-specific content, you need to implement custom
// logic to determine whether a request is cacheable based on its url and
// headers.
// 10-minute microcache.
// https://www.nginx.com/blog/benefits-of-microcaching-nginx/
const isStore = req => !!req.params && !!req.params[1] && req.params[1].includes('store')
const cacheMiddleware = microcache.cacheSeconds(10 * 60, req => useMicroCache && !isStore(req) && req.originalUrl)
function render (req, res) {
const s = Date.now()
res.setHeader('Content-Type', 'text/html')
res.setHeader('Server', serverInfo)
res.cookie('currentLanguage', req.params[0], {
maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days
})
const handleError = err => {
if (err.url) {
res.redirect(err.url)
} else if (err.code === 404) {
res.status(404).send('404 | Page Not Found')
} else {
// Render Error Page or Redirect
res.status(500).send('500 | Internal Server Error')
console.error(`error during render : ${req.url}`)
console.error(err.stack)
}
}
const context = {
title: 'Vuetify', // default title
url: req.url,
lang: req.params[0],
res,
hreflangs: availableLanguages.reduce((acc, lang) => {
return acc + `<link rel="alternate" hreflang="${lang}" href="https://${req.hostname}/${lang}${req.params[1]}" />`
}, '')
}
renderer.renderToString(context, (err, html) => {
if (err) {
return handleError(err)
}
res.end(html)
if (!isProd) {
console.log(`whole request: ${Date.now() - s}ms`)
}
})
}
const languageRegex = /^\/([a-z]{2,3}|[a-z]{2,3}-[a-zA-Z]{4}|[a-z]{2,3}-[A-Z]{2,3})(\/.*)?$/
app.get(languageRegex, isProd ? render : (req, res) => {
readyPromise.then(() => render(req, res))
}, cacheMiddleware)
// 302 redirect for no language
app.get('*', (req, res) => {
let lang = req.cookies.currentLanguage || req.acceptsLanguages(availableLanguages) || 'en'
if (!languageRegex.test('/' + lang)) lang = 'en'
res.redirect(302, `/${lang}${req.originalUrl}`)
})
const port = process.env.PORT || 8095
app.listen(port, '0.0.0.0', () => {
console.log(`server started at 0.0.0.0:${port}`)
})