How do I conditionally cache only non empty responses?
If you’re proxying an origin, you can use transformResponse
to set the statusCode to a 5xx. Any response with a status >= 400 will not be cached.
router.get('/', ({ proxy }) => {
proxy('origin', {
transformResponse(response) {
if (response.body.length === 0) {
response.statusCode = 500
}
}
})
})
Similarly, if you’re doing SSR on Layer0 serverless, you add the transformResponse
option to renderWithApp
. Here’s an example:
router.get('/', ({ renderWithApp }) => {
renderWithApp({
transformResponse(response) {
if (response.body.length === 0) {
response.statusCode = 500
}
}
})
})