From 5cc0b1a598004b9bad506d7b31058f62a8fa1437 Mon Sep 17 00:00:00 2001 From: xliusstk <48845925+xliusstk@users.noreply.github.com> Date: Fri, 22 May 2020 12:29:37 -0400 Subject: [PATCH] Fix ssr-caching example (#12180) cacheable-response.get expects a get(req, res) method signature: https://www.npmjs.com/package/cacheable-response#get Before the change: ``` curl -s -v http://localhost:3000/blog/first 2>&1 | grep HTTP/1.1 > GET /blog/first HTTP/1.1 < HTTP/1.1 404 Not Found ``` After the change: ``` curl -s -v http://localhost:3000/blog/first 2>&1 | grep HTTP/1.1 > GET /blog/first HTTP/1.1 < HTTP/1.1 200 OK ``` This is a partial fix of https://github.com/zeit/next.js/issues/12019 to make the example work. However it doesn't fix the error ``` (node:62360) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client ``` , which will need a change at server/next-server.ts See related issue: https://github.com/zeit/next.js/discussions/11525 https://github.com/zeit/next.js/issues/11665 --- examples/ssr-caching/server.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/ssr-caching/server.js b/examples/ssr-caching/server.js index 6e309cab3dbfa4..333aca98bbcc00 100644 --- a/examples/ssr-caching/server.js +++ b/examples/ssr-caching/server.js @@ -10,8 +10,11 @@ const handle = app.getRequestHandler() const ssrCache = cacheableResponse({ ttl: 1000 * 60 * 60, // 1hour - get: async ({ req, res, pagePath, queryParams }) => { - const data = await app.renderToHTML(req, res, pagePath, queryParams) + get: async ({ req, res }) => { + const data = await app.renderToHTML(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: @@ -28,12 +31,10 @@ const ssrCache = cacheableResponse({ app.prepare().then(() => { const server = express() - server.get('/', (req, res) => ssrCache({ req, res, pagePath: '/' })) + server.get('/', (req, res) => ssrCache({ req, res })) server.get('/blog/:id', (req, res) => { - const queryParams = { id: req.params.id } - const pagePath = '/blog' - return ssrCache({ req, res, pagePath, queryParams }) + return ssrCache({ req, res }) }) server.get('*', (req, res) => handle(req, res))