@@ -4,7 +4,12 @@ import type { NextConfigComplete } from 'next/dist/server/config-shared.js'
4
4
import type { WorkerRequestHandler } from 'next/dist/server/lib/types.js'
5
5
6
6
import { TagsManifest , getTagsManifest } from '../config.js'
7
- import { setCacheControlHeaders , setCacheTagsHeaders , setVaryHeaders } from '../headers.js'
7
+ import {
8
+ adjustDateHeader ,
9
+ setCacheControlHeaders ,
10
+ setCacheTagsHeaders ,
11
+ setVaryHeaders ,
12
+ } from '../headers.js'
8
13
import { nextResponseProxy } from '../revalidate.js'
9
14
10
15
let nextHandler : WorkerRequestHandler , nextConfig : NextConfigComplete , tagsManifest : TagsManifest
@@ -30,15 +35,6 @@ export default async (request: Request) => {
30
35
31
36
const resProxy = nextResponseProxy ( res )
32
37
33
- resProxy . prependListener ( '_headersSent' , ( event : HeadersSentEvent ) => {
34
- const headers = new Headers ( event . headers )
35
- setCacheControlHeaders ( headers )
36
- setCacheTagsHeaders ( headers , request , tagsManifest )
37
- setVaryHeaders ( headers , request , nextConfig )
38
- event . headers = Object . fromEntries ( headers . entries ( ) )
39
- // console.log('Modified response headers:', JSON.stringify(event.headers, null, 2))
40
- } )
41
-
42
38
// temporary workaround for https://linear.app/netlify/issue/ADN-111/
43
39
delete req . headers [ 'accept-encoding' ]
44
40
@@ -51,13 +47,16 @@ export default async (request: Request) => {
51
47
resProxy . end ( 'Internal Server Error' )
52
48
}
53
49
54
- // log the response from Next.js
55
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
56
- const response = {
57
- headers : resProxy . getHeaders ( ) ,
58
- statusCode : resProxy . statusCode ,
59
- }
60
- // console.log('Next server response:', JSON.stringify(response, null, 2))
50
+ // Contrary to the docs, this resolves when the headers are available, not when the stream closes.
51
+ // See https://github.com/fastly/http-compute-js/blob/main/src/http-compute-js/http-server.ts#L168-L173
52
+ const response = await toComputeResponse ( resProxy )
53
+
54
+ await adjustDateHeader ( response . headers , request )
55
+
56
+ setCacheControlHeaders ( response . headers )
57
+ setCacheTagsHeaders ( response . headers , request , tagsManifest )
58
+ setVaryHeaders ( response . headers , request , nextConfig )
59
+
61
60
62
- return toComputeResponse ( resProxy )
61
+ return response
63
62
}
0 commit comments