Skip to content

Commit

Permalink
fix(cache): abort cache handler for error status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 18, 2022
1 parent 9eb5769 commit ba21751
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ export function defineCachedEventHandler <T=any> (
event.context = incomingEvent.context
const body = await handler(event) || _resSendBody

// Abort cache if status code is error
// (Fine: 100 info, 200 okay, 300 redirect)
// (Not fine: 400: client error, 500: server error)
if (event.res.statusCode >= 400) {
throw new Error(`[nitro] Request failed with status code ${event.res.statusCode} for ${event.req.url}`)
}

// Collect cachable headers
const headers = event.res.getHeaders()
headers.etag = headers.Etag || headers.etag || `W/"${hash(body)}"`
Expand Down

0 comments on commit ba21751

Please sign in to comment.