Skip to content

Commit

Permalink
Do not cache 404 SSR responses (#10596)
Browse files Browse the repository at this point in the history
This change updates the _ssr-caching_ to not cache the response when a
404 status code has been returned.
  • Loading branch information
fabianishere committed Feb 19, 2020
1 parent 9fdd344 commit 72a461f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions examples/ssr-caching/server.js
Expand Up @@ -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),
})

Expand Down

0 comments on commit 72a461f

Please sign in to comment.