summaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/src/netlify-functions.ts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2022-04-12 16:50:10 -0400
committerGravatar GitHub <noreply@github.com> 2022-04-12 16:50:10 -0400
commita5caf08e2494e9f779baa6b288d277490dd436b8 (patch)
tree03b180be8156ef1215c2f801c22fdc94c33d0c54 /packages/integrations/netlify/src/netlify-functions.ts
parentc459c87325c4dad5fbcc62f2738c37ec543b02fc (diff)
downloadastro-a5caf08e2494e9f779baa6b288d277490dd436b8.tar.gz
astro-a5caf08e2494e9f779baa6b288d277490dd436b8.tar.zst
astro-a5caf08e2494e9f779baa6b288d277490dd436b8.zip
Allow setting multiple cookies in Netlify adapter (#3092)
* Allow setting multiple cookies in Netlify adapter * Adds a changeset * Set the response status code * Add a comment on why this is needed
Diffstat (limited to 'packages/integrations/netlify/src/netlify-functions.ts')
-rw-r--r--packages/integrations/netlify/src/netlify-functions.ts31
1 files changed, 27 insertions, 4 deletions
diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts
index 474cac4b6..bdfb78b8e 100644
--- a/packages/integrations/netlify/src/netlify-functions.ts
+++ b/packages/integrations/netlify/src/netlify-functions.ts
@@ -33,14 +33,37 @@ export const createExports = (manifest: SSRManifest, args: Args) => {
};
}
- const response = await app.render(request);
+ const response: Response = await app.render(request);
const responseBody = await response.text();
- return {
- statusCode: 200,
- headers: Object.fromEntries(response.headers.entries()),
+ const responseHeaders = Object.fromEntries(response.headers.entries());
+ const fnResponse: any = {
+ statusCode: response.status,
+ headers: responseHeaders,
body: responseBody,
};
+
+ // Special-case set-cookie which has to be set an different way :/
+ // The fetch API does not have a way to get multiples of a single header, but instead concatenates
+ // them. There are non-standard ways to do it, and node-fetch gives us headers.raw()
+ // See https://github.com/whatwg/fetch/issues/973 for discussion
+ if (response.headers.has('set-cookie') && 'raw' in response.headers) {
+ // Node fetch allows you to get the raw headers, which includes multiples of the same type.
+ // This is needed because Set-Cookie *must* be called for each cookie, and can't be
+ // concatenated together.
+ type HeadersWithRaw = Headers & {
+ raw: () => Record<string, string[]>;
+ };
+
+ const rawPacked = (response.headers as HeadersWithRaw).raw();
+ if('set-cookie' in rawPacked) {
+ fnResponse.multiValueHeaders = {
+ 'set-cookie': rawPacked['set-cookie']
+ }
+ }
+ }
+
+ return fnResponse;
};
return { handler };