From 2cc9d50524ac8c4e06a40ca4521881b8586ca043 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 25 Oct 2021 20:30:57 +0200 Subject: [PATCH 1/4] NextResponse: add JSON support --- packages/next/server/web/spec-extension/response.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/next/server/web/spec-extension/response.ts b/packages/next/server/web/spec-extension/response.ts index dabf3827d4ef2..6123bb1bac63e 100644 --- a/packages/next/server/web/spec-extension/response.ts +++ b/packages/next/server/web/spec-extension/response.ts @@ -13,7 +13,15 @@ export class NextResponse extends Response { url?: NextURL } - constructor(body?: BodyInit | null, init: ResponseInit = {}) { + constructor(body?: BodyInit | object | null, init: ResponseInit = {}) { + if (typeof body === 'object') { + body = JSON.stringify(body) + init.headers = { + ...init.headers, + 'content-type': 'application/json; charset=UTF-8', + } + } + super(body, init) const cookieParser = () => { From 987257d53930109da2d68a1428549b53449d3b38 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 25 Oct 2021 21:18:35 +0200 Subject: [PATCH 2/4] Update packages/next/server/web/spec-extension/response.ts Co-authored-by: JJ Kasper --- packages/next/server/web/spec-extension/response.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/server/web/spec-extension/response.ts b/packages/next/server/web/spec-extension/response.ts index 6123bb1bac63e..6214d41787ec5 100644 --- a/packages/next/server/web/spec-extension/response.ts +++ b/packages/next/server/web/spec-extension/response.ts @@ -14,7 +14,7 @@ export class NextResponse extends Response { } constructor(body?: BodyInit | object | null, init: ResponseInit = {}) { - if (typeof body === 'object') { + if (body && typeof body === 'object') { body = JSON.stringify(body) init.headers = { ...init.headers, From 85cb2f53eae4f38cce2af40f818b5e0de412039b Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 25 Oct 2021 21:18:41 +0200 Subject: [PATCH 3/4] Update packages/next/server/web/spec-extension/response.ts Co-authored-by: Steven --- packages/next/server/web/spec-extension/response.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/server/web/spec-extension/response.ts b/packages/next/server/web/spec-extension/response.ts index 6214d41787ec5..9083c8e92de87 100644 --- a/packages/next/server/web/spec-extension/response.ts +++ b/packages/next/server/web/spec-extension/response.ts @@ -18,7 +18,7 @@ export class NextResponse extends Response { body = JSON.stringify(body) init.headers = { ...init.headers, - 'content-type': 'application/json; charset=UTF-8', + 'content-type': 'application/json; charset=utf-8', } } From b73dfe9f7c79fc54d2e474465b7a39f924129cab Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 25 Oct 2021 21:20:28 +0200 Subject: [PATCH 4/4] ensure body is not a buffer --- packages/next/server/web/spec-extension/response.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/server/web/spec-extension/response.ts b/packages/next/server/web/spec-extension/response.ts index 9083c8e92de87..1682044e8dd59 100644 --- a/packages/next/server/web/spec-extension/response.ts +++ b/packages/next/server/web/spec-extension/response.ts @@ -14,7 +14,7 @@ export class NextResponse extends Response { } constructor(body?: BodyInit | object | null, init: ResponseInit = {}) { - if (body && typeof body === 'object') { + if (body && typeof body === 'object' && !Buffer.isBuffer(objet)) { body = JSON.stringify(body) init.headers = { ...init.headers,