Skip to content

Commit

Permalink
Remove setLazyProp for cookies for all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Sep 14, 2023
1 parent 7efe257 commit 65fcb56
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 0 additions & 2 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 9 additions & 1 deletion packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -386,6 +391,9 @@ export async function renderToHTMLImpl(
renderOpts: Omit<RenderOpts, keyof RenderOptsExtra>,
extra: RenderOptsExtra
): Promise<RenderResult> {
// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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': {
Expand Down

0 comments on commit 65fcb56

Please sign in to comment.