From 65da9a95593796c6834ff5b8a6ce9edc110e61c5 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Wed, 12 Oct 2022 12:34:04 +0200 Subject: [PATCH] actually make lazy --- packages/next/server/app-render.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/next/server/app-render.tsx b/packages/next/server/app-render.tsx index 44501a9dcdc42..e6b601b8287e5 100644 --- a/packages/next/server/app-render.tsx +++ b/packages/next/server/app-render.tsx @@ -83,6 +83,17 @@ const INTERNAL_COOKIES_INSTANCE = Symbol('internal for cookies readonly') function readonlyCookiesError() { return new Error('ReadonlyCookies cannot be modified') } + +function lazy(fn: () => T) { + let value: any + return () => { + if (!value) { + value = fn() + } + return value + } +} + class ReadonlyNextCookies { [INTERNAL_COOKIES_INSTANCE]: NextCookies @@ -1572,7 +1583,7 @@ export async function renderToHTMLOrFlight( ) const requestStore = { - headers: () => new ReadonlyHeaders(headersWithoutFlight(req.headers)), + headers: lazy(() => new ReadonlyHeaders(headersWithoutFlight(req.headers))), cookies: new ReadonlyNextCookies({ headers: { get: (key) => {