From a5bc73973a48c3d3b05db056d346b175d485362f Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Sun, 3 Nov 2024 03:33:59 +0400 Subject: [PATCH] avoid re-instantiating `Response` when not required --- .../templates/_worker.js/handleRequest.ts | 18 ++++++++++-------- .../templates/_worker.js/utils/routing.ts | 3 +++ 2 files changed, 13 insertions(+), 8 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..2bed3d860 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); + if (status || resp.status !== 101) { + const newHeaders = headers.normal; + applyHeaders(newHeaders, resp.headers); + applyHeaders(newHeaders, headers.important); - resp = new Response(resp.body, { - ...resp, - status: status || resp.status, - headers: newHeaders, - }); + resp = new Response(resp.body, { + ...resp, + status: status || resp.status, + headers: newHeaders, + }); + } return resp; } diff --git a/packages/next-on-pages/templates/_worker.js/utils/routing.ts b/packages/next-on-pages/templates/_worker.js/utils/routing.ts index 100741541..da3aa5e09 100644 --- a/packages/next-on-pages/templates/_worker.js/utils/routing.ts +++ b/packages/next-on-pages/templates/_worker.js/utils/routing.ts @@ -131,6 +131,9 @@ export async function runOrFetchBuildOutputItem( return new Response('Internal Server Error', { status: 500 }); } + if (resp.status === 101) { + return resp; + } return createMutableResponse(resp); }