@@ -118,6 +118,9 @@ export async function respond(request, options, state) {
118
118
/** @type {import('types').ResponseHeaders } */
119
119
const headers = { } ;
120
120
121
+ /** @type {string[] } */
122
+ const cookies = [ ] ;
123
+
121
124
/** @type {import('types').RequestEvent } */
122
125
const event = {
123
126
get clientAddress ( ) {
@@ -141,31 +144,26 @@ export async function respond(request, options, state) {
141
144
setHeaders : ( new_headers ) => {
142
145
for ( const key in new_headers ) {
143
146
const lower = key . toLowerCase ( ) ;
147
+ const value = new_headers [ key ] ;
144
148
145
- if ( lower in headers ) {
146
- if ( lower === 'set-cookie' ) {
147
- if ( ! Array . isArray ( headers [ lower ] ) ) {
148
- headers [ lower ] = [ /** @type {string } */ ( headers [ lower ] ) ] ;
149
- }
150
- const cookies = /** @type {string[] } */ ( headers [ lower ] ) ;
151
- const new_cookies = /** @type {string[] } */ (
152
- Array . isArray ( new_headers [ key ] ) ? new_headers [ key ] : [ new_headers [ key ] ]
153
- ) ;
154
- for ( const new_cookie of new_cookies ) {
155
- if ( cookies . includes ( new_cookie ) ) {
156
- throw new Error ( `"${ key } " header already has cookie with same value` ) ;
157
- }
158
- cookies . push ( new_cookie ) ;
149
+ if ( lower === 'set-cookie' ) {
150
+ const new_cookies = /** @type {string[] } */ ( Array . isArray ( value ) ? value : [ value ] ) ;
151
+
152
+ for ( const cookie of new_cookies ) {
153
+ if ( cookies . includes ( cookie ) ) {
154
+ throw new Error ( `"${ key } " header already has cookie with same value` ) ;
159
155
}
160
- } else {
161
- throw new Error ( `" ${ key } " header is already set` ) ;
156
+
157
+ cookies . push ( cookie ) ;
162
158
}
159
+ } else if ( lower in headers ) {
160
+ throw new Error ( `"${ key } " header is already set` ) ;
163
161
} else {
164
- headers [ lower ] = new_headers [ key ] ;
165
- }
162
+ headers [ lower ] = value ;
166
163
167
- if ( state . prerendering && lower === 'cache-control' ) {
168
- state . prerendering . cache = /** @type {string } */ ( new_headers [ key ] ) ;
164
+ if ( state . prerendering && lower === 'cache-control' ) {
165
+ state . prerendering . cache = /** @type {string } */ ( value ) ;
166
+ }
169
167
}
170
168
}
171
169
} ,
@@ -327,19 +325,19 @@ export async function respond(request, options, state) {
327
325
: await render_page ( event , route , options , state , resolve_opts ) ;
328
326
}
329
327
330
- for ( const key in headers ) {
331
- const value = headers [ key ] ;
332
- if ( key === 'set-cookie' ) {
333
- for ( const cookie of Array . isArray ( value ) ? value : [ value ] ) {
334
- response . headers . append ( key , /** @type {string } */ ( cookie ) ) ;
335
- }
336
- } else if ( ! is_data_request ) {
337
- // we only want to set cookies on __data.json requests, we don't
338
- // want to cache stuff erroneously etc
328
+ if ( ! is_data_request ) {
329
+ // we only want to set cookies on __data.json requests, we don't
330
+ // want to cache stuff erroneously etc
331
+ for ( const key in headers ) {
332
+ const value = headers [ key ] ;
339
333
response . headers . set ( key , /** @type {string } */ ( value ) ) ;
340
334
}
341
335
}
342
336
337
+ for ( const cookie of cookies ) {
338
+ response . headers . append ( 'set-cookie' , cookie ) ;
339
+ }
340
+
343
341
// respond with 304 if etag matches
344
342
if ( response . status === 200 && response . headers . has ( 'etag' ) ) {
345
343
let if_none_match_value = request . headers . get ( 'if-none-match' ) ;
0 commit comments