This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
99 lines (80 loc) · 2.64 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
const dev = process.env.NODE_ENV !== 'production'
// const goal = process.env.NODE_ENV
const next = require('next')
const express = require('express')
const LRUCache = require('lru-cache')
const helmet = require('helmet')
const mobxReact = require('mobx-react')
// const R = require('ramda')
const app = next({ dev, quiet: false })
const handle = app.getRequestHandler()
const SERVE_PORT = process.env.SERVE_PORT || 3001
// This is where we cache our rendered HTML pages
const ssrCache = new LRUCache({
max: 1000, // cache item count
// maxAge: 1000 * 60 * 60, // 1hour
maxAge: 1000 * 10, // 30 ses
})
mobxReact.useStaticRendering(true)
const HOME_PAGE = '/communities'
app.prepare().then(() => {
const server = express()
server.use(express.static('static'))
server.use(helmet())
server.get('/_next/:page?', (req, res) => handle(req, res))
server.get('/', (req, res) => {
// fconsole.log('match me root')
return res.redirect(HOME_PAGE)
})
server.get('/communities/:sub?', (req, res) => {
// console.log('match me user')
// return app.render(req, res, '/user', req.query)
return renderAndCache(req, res, '/communities', req.query)
})
server.get('/users/:sub?', (req, res) => {
// console.log('match me user')
// return app.render(req, res, '/user', req.query)
return renderAndCache(req, res, '/users', req.query)
})
server.get('/:community/:thread', (req, res) => {
// console.log('match me user')
// return app.render(req, res, '/user', req.query)
return renderAndCache(req, res, '/community', req.query)
})
server.get('*', (req, res) => handle(req, res))
server.listen(SERVE_PORT, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${SERVE_PORT}`)
})
})
/*
* NB: make sure to modify this to take into account anything that should trigger
* an immediate page change (e.g a locale stored in req.session)
*/
function getCacheKey(req) {
return `${req.url}`
}
async function renderAndCache(req, res, pagePath, queryParams) {
const key = getCacheKey(req)
// If we have a page in the cache, let's serve it
if (ssrCache.has(key)) {
res.setHeader('x-cache', 'HIT')
res.send(ssrCache.get(key))
return
}
try {
// If not let's render the page into HTML
const html = await app.renderToHTML(req, res, pagePath, queryParams)
// Something is wrong with the request, let's skip the cache
if (res.statusCode !== 200) {
res.send(html)
return
}
// Let's cache this page
ssrCache.set(key, html)
res.setHeader('x-cache', 'MISS')
res.send(html)
} catch (err) {
app.renderError(err, req, res, pagePath, queryParams)
}
}