1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/ssr-caching/server.js
Sébastien Dubois 3c7237169f ssr-caching: document way to configure cache key (#1133)
* ssr-caching: document way to configure cache key

* simplify comment
2017-02-14 22:01:50 +05:30

70 lines
1.7 KiB
JavaScript

const express = require('express')
const next = require('next')
const LRUCache = require('lru-cache')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dir: '.', dev })
const handle = app.getRequestHandler()
// This is where we cache our rendered HTML pages
const ssrCache = new LRUCache({
max: 100,
maxAge: 1000 * 60 * 60 // 1hour
})
app.prepare()
.then(() => {
const server = express()
// Use the `renderAndCache` utility defined below to serve pages
server.get('/', (req, res) => {
renderAndCache(req, res, '/')
})
server.get('/blog/:id', (req, res) => {
const queryParams = { id: req.params.id }
renderAndCache(req, res, '/blog', queryParams)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
/*
* 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}`
}
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)) {
console.log(`CACHE HIT: ${key}`)
res.send(ssrCache.get(key))
return
}
// If not let's render the page into HTML
app.renderToHTML(req, res, pagePath, queryParams)
.then((html) => {
// Let's cache this page
console.log(`CACHE MISS: ${key}`)
ssrCache.set(key, html)
res.send(html)
})
.catch((err) => {
app.renderError(err, req, res, pagePath, queryParams)
})
}