From a898b838195fe385f932a8310b3880a14f40618b Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 14 Sep 2023 16:26:43 +0200 Subject: [PATCH] Remove setLazyProp for cookies for all requests --- packages/next/src/server/base-server.ts | 2 -- .../next/src/server/lib/router-utils/resolve-routes.ts | 1 - packages/next/src/server/render.tsx | 10 +++++++++- .../src/shared/lib/router/utils/prepare-destination.ts | 9 ++++++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index 4e6ed0a307a49b..0a2583b962dfd8 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -791,8 +791,6 @@ export default abstract class Server { return } - setLazyProp({ req: req as any }, 'cookies', getCookieParser(req.headers)) - // Parse url if parsedUrl not provided if (!parsedUrl || typeof parsedUrl !== 'object') { parsedUrl = parseUrl(req.url!, true) diff --git a/packages/next/src/server/lib/router-utils/resolve-routes.ts b/packages/next/src/server/lib/router-utils/resolve-routes.ts index 11097b180bc8ce..dab71210053a1e 100644 --- a/packages/next/src/server/lib/router-utils/resolve-routes.ts +++ b/packages/next/src/server/lib/router-utils/resolve-routes.ts @@ -154,7 +154,6 @@ export function getResolveRoutes( addRequestMeta(req, '__NEXT_INIT_URL', initUrl) addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query }) addRequestMeta(req, '_protocol', protocol) - setLazyProp({ req }, 'cookies', () => getCookieParser(req.headers)()) if (!isUpgradeReq) { addRequestMeta(req, '__NEXT_CLONABLE_BODY', getCloneableBody(req)) diff --git a/packages/next/src/server/render.tsx b/packages/next/src/server/render.tsx index 9a9843d9e32ec4..053ba7d2b135ee 100644 --- a/packages/next/src/server/render.tsx +++ b/packages/next/src/server/render.tsx @@ -15,7 +15,12 @@ import type { } from '../shared/lib/utils' import type { ImageConfigComplete } from '../shared/lib/image-config' import type { Redirect } from '../lib/load-custom-routes' -import type { NextApiRequestCookies, __ApiPreviewProps } from './api-utils' +import { + getCookieParser, + type NextApiRequestCookies, + type __ApiPreviewProps, + setLazyProp, +} from './api-utils' import type { FontManifest, FontConfig } from './font-utils' import type { LoadComponentsReturnType, ManifestItem } from './load-components' import type { @@ -386,6 +391,9 @@ export async function renderToHTMLImpl( renderOpts: Omit, extra: RenderOptsExtra ): Promise { + // Adds support for reading `cookies` in `getServerSideProps` when SSR. + setLazyProp({ req: req as any }, 'cookies', getCookieParser(req.headers)) + const renderResultMeta: RenderResultMetadata = {} // In dev we invalidate the cache by appending a timestamp to the resource URL. diff --git a/packages/next/src/shared/lib/router/utils/prepare-destination.ts b/packages/next/src/shared/lib/router/utils/prepare-destination.ts index 6ebb5d5f898c9d..4a290fd5c34abd 100644 --- a/packages/next/src/shared/lib/router/utils/prepare-destination.ts +++ b/packages/next/src/shared/lib/router/utils/prepare-destination.ts @@ -13,6 +13,7 @@ import { isInterceptionRouteAppPath, } from '../../../../server/future/helpers/interception-routes' import { NEXT_RSC_UNION_QUERY } from '../../../../client/components/app-router-headers' +import { getCookieParser } from '../../../../server/api-utils' /** * Ensure only a-zA-Z are used for param names for proper interpolating @@ -64,7 +65,13 @@ export function matchHas( break } case 'cookie': { - value = (req as any).cookies[hasItem.key] + if ('cookies' in req) { + value = req.cookies[hasItem.key] + } else { + const cookies = getCookieParser(req.headers)() + value = cookies[hasItem.key] + } + break } case 'query': {