From 72a461f913bb578951cb5f7ff23d569a8e807470 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 19 Feb 2020 16:59:37 +0100 Subject: [PATCH] Do not cache 404 SSR responses (#10596) This change updates the _ssr-caching_ to not cache the response when a 404 status code has been returned. --- examples/ssr-caching/server.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/ssr-caching/server.js b/examples/ssr-caching/server.js index f58286f67487..0fc217c8f4b9 100644 --- a/examples/ssr-caching/server.js +++ b/examples/ssr-caching/server.js @@ -10,9 +10,18 @@ const handle = app.getRequestHandler() const ssrCache = cacheableResponse({ ttl: 1000 * 60 * 60, // 1hour - get: async ({ req, res, pagePath, queryParams }) => ({ - data: await app.renderToHTML(req, res, pagePath, queryParams), - }), + get: async ({ req, res, pagePath, queryParams }) => { + const data = await app.renderToHTML(req, res, pagePath, queryParams) + + // Add here custom logic for when you do not want to cache the page, for + // example when the page returns a 404 status code: + if (res.statusCode === 404) { + res.end(data) + return + } + + return { data } + }, send: ({ data, res }) => res.send(data), })