Skip to content

Commit

Permalink
Change type of GetServerSidePropsContext.req.cookies the be the same …
Browse files Browse the repository at this point in the history
…as NextApiRequest.cookies (#21336)

The original type for `GetServerSidePropsContext.req.cookies` was introduced in #19724. In the PR, the test at `test/integration/typescript/test/index.test.js` expects req.cookies to always exist, so `req.cookies` will never be undefined.

I'm guessing that req.cookies is parsed using the same cookie middleware as `NextApiRequest`, in which case `req.cookies` should be `{ [key: string]: string }`, not `{ [key: string]: any }`.
  • Loading branch information
andrehsu authored Jan 26, 2021
1 parent f6cc0de commit a47d47e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ import {
NextComponentType,
RenderPage,
} from '../lib/utils'
import { tryGetPreviewData, __ApiPreviewProps } from './api-utils'
import {
tryGetPreviewData,
NextApiRequestCookies,
__ApiPreviewProps,
} from './api-utils'
import { denormalizePagePath } from './denormalize-page-path'
import { FontManifest, getFontDefinitionFromManifest } from './font-utils'
import { LoadComponentsReturnType, ManifestItem } from './load-components'
Expand Down Expand Up @@ -796,7 +800,9 @@ export async function renderToHTML(

try {
data = await getServerSideProps({
req,
req: req as IncomingMessage & {
cookies: NextApiRequestCookies
},
res,
query,
resolvedUrl: renderOpts.resolvedUrl as string,
Expand Down
7 changes: 6 additions & 1 deletion packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
// @ts-ignore This path is generated at build time and conflicts otherwise
} from '../dist/next-server/lib/utils'

import {
NextApiRequestCookies,
// @ts-ignore This path is generated at build time and conflicts otherwise
} from '../dist/next-server/server/api-utils'

// @ts-ignore This path is generated at build time and conflicts otherwise
import next from '../dist/server/next'

Expand Down Expand Up @@ -130,7 +135,7 @@ export type GetServerSidePropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery
> = {
req: IncomingMessage & {
cookies?: { [key: string]: any }
cookies: NextApiRequestCookies
}
res: ServerResponse
params?: Q
Expand Down

0 comments on commit a47d47e

Please sign in to comment.