@@ -33,14 +33,37 @@ export const createExports = (manifest: SSRManifest, args: Args) => {
3333 } ;
3434 }
3535
36- const response = await app . render ( request ) ;
36+ const response : Response = await app . render ( request ) ;
3737 const responseBody = await response . text ( ) ;
3838
39- return {
40- statusCode : 200 ,
41- headers : Object . fromEntries ( response . headers . entries ( ) ) ,
39+ const responseHeaders = Object . fromEntries ( response . headers . entries ( ) ) ;
40+ const fnResponse : any = {
41+ statusCode : response . status ,
42+ headers : responseHeaders ,
4243 body : responseBody ,
4344 } ;
45+
46+ // Special-case set-cookie which has to be set an different way :/
47+ // The fetch API does not have a way to get multiples of a single header, but instead concatenates
48+ // them. There are non-standard ways to do it, and node-fetch gives us headers.raw()
49+ // See https://github.com/whatwg/fetch/issues/973 for discussion
50+ if ( response . headers . has ( 'set-cookie' ) && 'raw' in response . headers ) {
51+ // Node fetch allows you to get the raw headers, which includes multiples of the same type.
52+ // This is needed because Set-Cookie *must* be called for each cookie, and can't be
53+ // concatenated together.
54+ type HeadersWithRaw = Headers & {
55+ raw : ( ) => Record < string , string [ ] > ;
56+ } ;
57+
58+ const rawPacked = ( response . headers as HeadersWithRaw ) . raw ( ) ;
59+ if ( 'set-cookie' in rawPacked ) {
60+ fnResponse . multiValueHeaders = {
61+ 'set-cookie' : rawPacked [ 'set-cookie' ]
62+ }
63+ }
64+ }
65+
66+ return fnResponse ;
4467 } ;
4568
4669 return { handler } ;
0 commit comments