From ab0c9a91e116c9c8973220e66bda2660270bfa49 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Sat, 2 Nov 2024 21:14:22 +0330 Subject: [PATCH] avoid re-instantiating `Response` when not required --- .../templates/_worker.js/handleRequest.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/next-on-pages/templates/_worker.js/handleRequest.ts b/packages/next-on-pages/templates/_worker.js/handleRequest.ts index 418ba249d..ec4e6ec9d 100644 --- a/packages/next-on-pages/templates/_worker.js/handleRequest.ts +++ b/packages/next-on-pages/templates/_worker.js/handleRequest.ts @@ -114,15 +114,17 @@ async function generateResponse( }); } - const newHeaders = headers.normal; - applyHeaders(newHeaders, resp.headers); - applyHeaders(newHeaders, headers.important); - - resp = new Response(resp.body, { - ...resp, - status: status || resp.status, - headers: newHeaders, - }); + if (status !== null || [...headers.normal].length > 0 || [...headers.important].length > 0) { + const newHeaders = headers.normal; + applyHeaders(newHeaders, resp.headers); + applyHeaders(newHeaders, headers.important); + + resp = new Response(resp.body, { + ...resp, + status: status || resp.status, + headers: newHeaders, + }); + } return resp; }