@@ -113,19 +113,7 @@ export default async (request: Request) => {
113
setVaryHeaders ( response . headers , request , nextConfig )
113
setVaryHeaders ( response . headers , request , nextConfig )
114
setCacheStatusHeader ( response . headers )
114
setCacheStatusHeader ( response . headers )
115
115
116
- // Temporary workaround for an issue where sending a response with an empty
116
+ async function waitForBackgroundWork ( ) {
117
- // body causes an unhandled error. This doesn't catch everything, but redirects are the
118
- // most common case of sending empty bodies. We can't check it directly because these are streams.
119
- // The side effect is that responses which do contain data will not be streamed to the client,
120
- // but that's fine for redirects.
121
- // TODO: Remove once a fix has been rolled out.
122
- if ( ( response . status > 300 && response . status < 400 ) || response . status >= 500 ) {
123
- const body = await response . text ( )
124
- return new Response ( body || null , response )
125
- }
126
-
127
- const keepOpenUntilNextFullyRendered = new TransformStream ( {
128
- async flush ( ) {
129
// it's important to keep the stream open until the next handler has finished
117
// it's important to keep the stream open until the next handler has finished
130
await nextHandlerPromise
118
await nextHandlerPromise
131
119
@@ -138,9 +126,18 @@ export default async (request: Request) => {
138
// are resolved. If `context.waitUntil` is available, `requestContext.backgroundWorkPromise` will be empty
126
// are resolved. If `context.waitUntil` is available, `requestContext.backgroundWorkPromise` will be empty
139
// resolved promised and so awaiting it is no-op
127
// resolved promised and so awaiting it is no-op
140
await requestContext . backgroundWorkPromise
128
await requestContext . backgroundWorkPromise
129
+ }
130
+
131
+ const keepOpenUntilNextFullyRendered = new TransformStream ( {
132
+ async flush ( ) {
133
+ await waitForBackgroundWork ( )
141
} ,
134
} ,
142
} )
135
} )
143
136
137
+ if ( ! response . body ) {
138
+ await waitForBackgroundWork ( )
139
+ }
140
+
144
return new Response ( response . body ?. pipeThrough ( keepOpenUntilNextFullyRendered ) , response )
141
return new Response ( response . body ?. pipeThrough ( keepOpenUntilNextFullyRendered ) , response )
145
} )
142
} )
146
}
143
}
0 commit comments