@@ -46,50 +46,55 @@ export default async (request: Request) => {
46
46
} )
47
47
}
48
48
49
- const { req, res } = toReqRes ( request )
49
+ return await tracer . startActiveSpan ( 'generate response' , async ( span ) => {
50
+ const { req, res } = toReqRes ( request )
50
51
51
- const resProxy = nextResponseProxy ( res )
52
+ const resProxy = nextResponseProxy ( res )
52
53
53
- // temporary workaround for https://linear.app/netlify/issue/ADN-111/
54
- delete req . headers [ 'accept-encoding' ]
54
+ // temporary workaround for https://linear.app/netlify/issue/ADN-111/
55
+ delete req . headers [ 'accept-encoding' ]
55
56
56
- // We don't await this here, because it won't resolve until the response is finished.
57
- const nextHandlerPromise = nextHandler ( req , resProxy ) . catch ( ( error ) => {
58
- logger . withError ( error ) . error ( 'next handler error' )
59
- console . error ( error )
60
- resProxy . statusCode = 500
61
- resProxy . end ( 'Internal Server Error' )
62
- } )
57
+ // We don't await this here, because it won't resolve until the response is finished.
58
+ const nextHandlerPromise = nextHandler ( req , resProxy ) . catch ( ( error ) => {
59
+ logger . withError ( error ) . error ( 'next handler error' )
60
+ console . error ( error )
61
+ resProxy . statusCode = 500
62
+ resProxy . end ( 'Internal Server Error' )
63
+ } )
63
64
64
- // Contrary to the docs, this resolves when the headers are available, not when the stream closes.
65
- // See https://github.com/fastly/http-compute-js/blob/main/src/http-compute-js/http-server.ts#L168-L173
66
- const response = await toComputeResponse ( resProxy )
67
-
68
- await adjustDateHeader ( response . headers , request )
69
-
70
- setCacheControlHeaders ( response . headers , request )
71
- setCacheTagsHeaders ( response . headers , request , tagsManifest )
72
- setVaryHeaders ( response . headers , request , nextConfig )
73
- handleNextCacheHeader ( response . headers )
74
-
75
- // Temporary workaround for an issue where sending a response with an empty
76
- // body causes an unhandled error. This doesn't catch everything, but redirects are the
77
- // most common case of sending empty bodies. We can't check it directly because these are streams.
78
- // The side effect is that responses which do contain data will not be streamed to the client,
79
- // but that's fine for redirects.
80
- // TODO: Remove once a fix has been rolled out.
81
- if ( ( response . status > 300 && response . status < 400 ) || response . status >= 500 ) {
82
- const body = await response . text ( )
83
- return new Response ( body || null , response )
84
- }
65
+ // Contrary to the docs, this resolves when the headers are available, not when the stream closes.
66
+ // See https://github.com/fastly/http-compute-js/blob/main/src/http-compute-js/http-server.ts#L168-L173
67
+ const response = await toComputeResponse ( resProxy )
68
+
69
+ await adjustDateHeader ( response . headers , request , span , tracer )
70
+
71
+ setCacheControlHeaders ( response . headers , request )
72
+ setCacheTagsHeaders ( response . headers , request , tagsManifest )
73
+ setVaryHeaders ( response . headers , request , nextConfig )
74
+ handleNextCacheHeader ( response . headers )
75
+
76
+ // Temporary workaround for an issue where sending a response with an empty
77
+ // body causes an unhandled error. This doesn't catch everything, but redirects are the
78
+ // most common case of sending empty bodies. We can't check it directly because these are streams.
79
+ // The side effect is that responses which do contain data will not be streamed to the client,
80
+ // but that's fine for redirects.
81
+ // TODO: Remove once a fix has been rolled out.
82
+ if ( ( response . status > 300 && response . status < 400 ) || response . status >= 500 ) {
83
+ const body = await response . text ( )
84
+ span . end ( )
85
+ return new Response ( body || null , response )
86
+ }
87
+
88
+ const keepOpenUntilNextFullyRendered = new TransformStream ( {
89
+ flush ( ) {
90
+ // it's important to keep the stream open until the next handler has finished,
91
+ // or otherwise the cache revalidates might not go through
92
+ return nextHandlerPromise . then ( ( ) => {
93
+ span . end ( )
94
+ } )
95
+ } ,
96
+ } )
85
97
86
- const keepOpenUntilNextFullyRendered = new TransformStream ( {
87
- flush ( ) {
88
- // it's important to keep the stream open until the next handler has finished,
89
- // or otherwise the cache revalidates might not go through
90
- return nextHandlerPromise
91
- } ,
98
+ return new Response ( response . body ?. pipeThrough ( keepOpenUntilNextFullyRendered ) , response )
92
99
} )
93
-
94
- return new Response ( response . body ?. pipeThrough ( keepOpenUntilNextFullyRendered ) , response )
95
100
}
0 commit comments