Skip to content

Commit

Permalink
ssr-caching: document way to configure cache key (#1133)
Browse files Browse the repository at this point in the history
* ssr-caching: document way to configure cache key

* simplify comment
  • Loading branch information
sedubois authored and arunoda committed Feb 14, 2017
1 parent 507e8c2 commit 3c72371
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions examples/ssr-caching/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,30 @@ 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
}

// 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: ${req.url}`)
ssrCache.set(req.url, html)
console.log(`CACHE MISS: ${key}`)
ssrCache.set(key, html)

res.send(html)
})
Expand Down

0 comments on commit 3c72371

Please sign in to comment.