mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
ssr-caching: document way to configure cache key (#1133)
* ssr-caching: document way to configure cache key * simplify comment
This commit is contained in:
parent
507e8c2ca4
commit
3c7237169f
|
@ -36,11 +36,21 @@ app.prepare()
|
|||
})
|
||||
})
|
||||
|
||||
/*
|
||||
* 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(req.url)) {
|
||||
console.log(`CACHE HIT: ${req.url}`)
|
||||
res.send(ssrCache.get(req.url))
|
||||
if (ssrCache.has(key)) {
|
||||
console.log(`CACHE HIT: ${key}`)
|
||||
res.send(ssrCache.get(key))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -48,8 +58,8 @@ function renderAndCache (req, res, pagePath, queryParams) {
|
|||
app.renderToHTML(req, res, pagePath, queryParams)
|
||||
.then((html) => {
|
||||
// Let's cache this page
|
||||
console.log(`CACHE MISS: ${req.url}`)
|
||||
ssrCache.set(req.url, html)
|
||||
console.log(`CACHE MISS: ${key}`)
|
||||
ssrCache.set(key, html)
|
||||
|
||||
res.send(html)
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue