Skip to content

Commit 4d58e10

Browse files
authored
[fix] Adapter netlify: don't set the request body on get/head requests (#3459)
* don't set the request body on get/head requests * add changeset
1 parent 799f1bc commit 4d58e10

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

.changeset/cool-bananas-share.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-netlify': patch
3+
---
4+
5+
Avoid setting the body of the request when the request method is GET or HEAD

packages/adapter-netlify/src/handler.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,7 @@ export function init(manifest) {
99
const app = new App(manifest);
1010

1111
return async (event) => {
12-
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;
13-
14-
const encoding = isBase64Encoded ? 'base64' : 'utf-8';
15-
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;
16-
17-
const rendered = await app.render(
18-
new Request(rawUrl, {
19-
method: httpMethod,
20-
headers: new Headers(headers),
21-
body: rawBody
22-
})
23-
);
12+
const rendered = await app.render(to_request(event));
2413

2514
const partial_response = {
2615
statusCode: rendered.status,
@@ -46,6 +35,27 @@ export function init(manifest) {
4635
};
4736
}
4837

38+
/**
39+
* @param {import('@netlify/functions').HandlerEvent} event
40+
* @returns {Request}
41+
*/
42+
function to_request(event) {
43+
const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;
44+
45+
/** @type {RequestInit} */
46+
const init = {
47+
method: httpMethod,
48+
headers: new Headers(headers)
49+
};
50+
51+
if (httpMethod !== 'GET' && httpMethod !== 'HEAD') {
52+
const encoding = isBase64Encoded ? 'base64' : 'utf-8';
53+
init.body = typeof body === 'string' ? Buffer.from(body, encoding) : body;
54+
}
55+
56+
return new Request(rawUrl, init);
57+
}
58+
4959
/**
5060
* Splits headers into two categories: single value and multi value
5161
* @param {Headers} headers

0 commit comments

Comments
 (0)