2016-12-23 17:13:52 +00:00
|
|
|
const express = require('express')
|
|
|
|
const next = require('next')
|
|
|
|
const LRUCache = require('lru-cache')
|
|
|
|
|
2017-08-10 18:15:46 +00:00
|
|
|
const port = parseInt(process.env.PORT, 10) || 3000
|
2016-12-28 18:12:24 +00:00
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
2018-05-13 22:00:37 +00:00
|
|
|
const app = next({ dev })
|
2016-12-23 17:13:52 +00:00
|
|
|
const handle = app.getRequestHandler()
|
|
|
|
|
|
|
|
// This is where we cache our rendered HTML pages
|
|
|
|
const ssrCache = new LRUCache({
|
2018-12-06 22:17:59 +00:00
|
|
|
length: function (n, key) {
|
|
|
|
return n.toString().length + key.toString().length
|
|
|
|
},
|
|
|
|
max: 100 * 1000 * 1000, // 100MB cache soft limit
|
2016-12-23 17:13:52 +00:00
|
|
|
maxAge: 1000 * 60 * 60 // 1hour
|
|
|
|
})
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
app.prepare().then(() => {
|
|
|
|
const server = express()
|
2016-12-23 17:13:52 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
// Use the `renderAndCache` utility defined below to serve pages
|
|
|
|
server.get('/', (req, res) => {
|
|
|
|
renderAndCache(req, res, '/')
|
|
|
|
})
|
2016-12-23 17:13:52 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.get('/blog/:id', (req, res) => {
|
|
|
|
const queryParams = { id: req.params.id }
|
|
|
|
renderAndCache(req, res, '/blog', queryParams)
|
|
|
|
})
|
2016-12-23 17:13:52 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.get('*', (req, res) => {
|
|
|
|
return handle(req, res)
|
|
|
|
})
|
2016-12-23 17:13:52 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.listen(port, err => {
|
|
|
|
if (err) throw err
|
|
|
|
console.log(`> Ready on http://localhost:${port}`)
|
2016-12-23 17:13:52 +00:00
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
})
|
2016-12-23 17:13:52 +00:00
|
|
|
|
2017-02-14 16:31:50 +00:00
|
|
|
/*
|
|
|
|
* 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}`
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:21:49 +00:00
|
|
|
async function renderAndCache (req, res, pagePath, queryParams) {
|
2017-02-14 16:31:50 +00:00
|
|
|
const key = getCacheKey(req)
|
|
|
|
|
2016-12-23 17:13:52 +00:00
|
|
|
// If we have a page in the cache, let's serve it
|
2017-02-14 16:31:50 +00:00
|
|
|
if (ssrCache.has(key)) {
|
2018-02-06 12:21:49 +00:00
|
|
|
res.setHeader('x-cache', 'HIT')
|
2017-02-14 16:31:50 +00:00
|
|
|
res.send(ssrCache.get(key))
|
2016-12-23 17:13:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:21:49 +00:00
|
|
|
try {
|
|
|
|
// If not let's render the page into HTML
|
|
|
|
const html = await app.renderToHTML(req, res, pagePath, queryParams)
|
2016-12-23 17:13:52 +00:00
|
|
|
|
2018-02-06 12:21:49 +00:00
|
|
|
// Something is wrong with the request, let's skip the cache
|
|
|
|
if (res.statusCode !== 200) {
|
2016-12-23 17:13:52 +00:00
|
|
|
res.send(html)
|
2018-02-06 12:21:49 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-12-23 17:13:52 +00:00
|
|
|
}
|