From 3c7237169f14b0d1e867a5373d068f0591fb5864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Dubois?= Date: Tue, 14 Feb 2017 18:31:50 +0200 Subject: [PATCH] ssr-caching: document way to configure cache key (#1133) * ssr-caching: document way to configure cache key * simplify comment --- examples/ssr-caching/server.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/ssr-caching/server.js b/examples/ssr-caching/server.js index 1d94c96d..1f16da64 100644 --- a/examples/ssr-caching/server.js +++ b/examples/ssr-caching/server.js @@ -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) })