-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
54 lines (45 loc) · 1.15 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
const express = require('express')
const next = require('next')
const { parse } = require('url')
const config = require('./config')
const LRUCache = require('lru-cache')
const { createReadStream } = require('fs')
const dev = process.env.NODE_ENV !== config.mode
const app = next({ dev })
const handle = app.getRequestHandler()
const ssrCache = new LRUCache({
max: 100,
maxAge: 10000 * 60 * 60,
})
app.prepare()
.then(() => {
const server = express()
server.get('/', (req, res) => {
renderAndCache(req, res, '/', req.query)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on ${config.path}:${config.port}`)
})
})
function getCacheKey (req) {
return `${req.url}`
}
function renderAndCache (req, res, pagePath, queryParams) {
const key = getCacheKey(req)
if (ssrCache.has(key)) {
res.send(ssrCache.get(key))
return
}
app.renderToHTML(req, res, pagePath, queryParams)
.then((html) => {
ssrCache.set(key, html)
res.send(html)
})
.catch((err) => {
app.renderError(err, req, res, pagePath, queryParams)
})
}