@@ -37,27 +37,31 @@ export default async function handleError(error, req, res, next) {
37
37
// Note, not using console.error() because it's arguably handled.
38
38
// Some tests might actually expect a 500 error.
39
39
40
+ const responseDone = res . headersSent || req . aborted
41
+
40
42
if ( req . path . startsWith ( '/assets' ) || req . path . startsWith ( '/_next/static' ) ) {
41
- // By default, Fastly will cache 404 responses unless otherwise
42
- // told not to.
43
- // See https://docs.fastly.com/en/guides/how-caching-and-cdns-work#http-status-codes-cached-by-default
44
- // Let's cache our 404'ing assets conservatively.
45
- // The Cache-Control is short, and let's use the default surrogate
46
- // key just in case it was a mistake.
47
- cacheControl ( res )
48
- // Undo the cookie setting that CSRF sets.
49
- res . removeHeader ( 'set-cookie' )
50
- // Makes sure the surrogate key is NOT the manual one if it failed.
51
- // This basically unsets what was assumed in the beginning of
52
- // loading all the middlewares.
53
- setFastlySurrogateKey ( res , SURROGATE_ENUMS . DEFAULT )
43
+ if ( ! responseDone ) {
44
+ // By default, Fastly will cache 404 responses unless otherwise
45
+ // told not to.
46
+ // See https://docs.fastly.com/en/guides/how-caching-and-cdns-work#http-status-codes-cached-by-default
47
+ // Let's cache our 404'ing assets conservatively.
48
+ // The Cache-Control is short, and let's use the default surrogate
49
+ // key just in case it was a mistake.
50
+ cacheControl ( res )
51
+ // Undo the cookie setting that CSRF sets.
52
+ res . removeHeader ( 'set-cookie' )
53
+ // Makes sure the surrogate key is NOT the manual one if it failed.
54
+ // This basically unsets what was assumed in the beginning of
55
+ // loading all the middlewares.
56
+ setFastlySurrogateKey ( res , SURROGATE_ENUMS . DEFAULT )
57
+ }
54
58
} else if ( process . env . NODE_ENV === 'test' ) {
55
59
console . warn ( 'An error occurrred in some middleware handler' , error )
56
60
}
57
61
58
62
try {
59
63
// If the headers have already been sent or the request was aborted...
60
- if ( res . headersSent || req . aborted ) {
64
+ if ( responseDone ) {
61
65
// Report to Failbot
62
66
await logException ( error , req )
63
67
0 commit comments