From 2dd8152c37d7ad0267af47a3db8fb5b508ef6d74 Mon Sep 17 00:00:00 2001 From: "gwanhyeong.lee" Date: Wed, 4 Nov 2020 21:08:33 +0900 Subject: [PATCH] Fix ssr caching example --- examples/ssr-caching/server.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/examples/ssr-caching/server.js b/examples/ssr-caching/server.js index 50f67faf8487a..68e72ea504cbf 100644 --- a/examples/ssr-caching/server.js +++ b/examples/ssr-caching/server.js @@ -11,18 +11,21 @@ const handle = app.getRequestHandler() const ssrCache = cacheableResponse({ ttl: 1000 * 60 * 60, // 1hour get: async ({ req, res }) => { - const data = await app.render(req, res, req.path, { - ...req.query, - ...req.params, + const rawResEnd = res.end + const data = await new Promise((resolve) => { + res.end = (payload) => { + if (res.statusCode === 200) { + resolve(payload) + } else { + resolve() + } + } + app.render(req, res, req.path, { + ...req.query, + ...req.params, + }) }) - - // 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 - } - + res.end = rawResEnd return { data } }, send: ({ data, res }) => res.send(data),