From 0a0f4b6909c101de471fdfcbf1ad15f7601fd67a Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 20 Jan 2022 15:15:58 -0300 Subject: [PATCH 1/2] don't set the request body on get/head requests --- packages/adapter-netlify/src/handler.js | 34 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/adapter-netlify/src/handler.js b/packages/adapter-netlify/src/handler.js index 0835a3f6beff..e75fdd97a85f 100644 --- a/packages/adapter-netlify/src/handler.js +++ b/packages/adapter-netlify/src/handler.js @@ -9,18 +9,7 @@ export function init(manifest) { const app = new App(manifest); return async (event) => { - const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event; - - const encoding = isBase64Encoded ? 'base64' : 'utf-8'; - const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body; - - const rendered = await app.render( - new Request(rawUrl, { - method: httpMethod, - headers: new Headers(headers), - body: rawBody - }) - ); + const rendered = await app.render(to_request(event)); const partial_response = { statusCode: rendered.status, @@ -46,6 +35,27 @@ export function init(manifest) { }; } +/** + * @param {import('@netlify/functions').HandlerEvent} event + * @returns {Request} + */ +function to_request(event) { + const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event; + + /** @type {RequestInit} */ + const init = { + method: httpMethod, + headers: new Headers(headers) + }; + + if (httpMethod !== 'GET' && httpMethod !== 'HEAD') { + const encoding = isBase64Encoded ? 'base64' : 'utf-8'; + init.body = typeof body === 'string' ? Buffer.from(body, encoding) : body; + } + + return new Request(rawUrl, init); +} + /** * Splits headers into two categories: single value and multi value * @param {Headers} headers From 6b779da4113a06a70485435649dc2e76d3e078e6 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 20 Jan 2022 15:23:56 -0300 Subject: [PATCH 2/2] add changeset --- .changeset/cool-bananas-share.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cool-bananas-share.md diff --git a/.changeset/cool-bananas-share.md b/.changeset/cool-bananas-share.md new file mode 100644 index 000000000000..08fc64971819 --- /dev/null +++ b/.changeset/cool-bananas-share.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-netlify': patch +--- + +Avoid setting the body of the request when the request method is GET or HEAD