Skip to content

Commit 7fd5a24

Browse files
authored
feat: add type to previewData (vercel#21574)
`previewData` was already [typed declare variable](https://github.com/vercel/next.js/compare/canary...tarunama:feature/remove-any1?expand=1#diff-bd7baf53ff559d84461af8b2fd62cade7e2d8eb203f489e24a27c5b83a79a9d3L1380). So I have defined `PreviewData` type, and adapt for avoiding error by type safe.
1 parent 41efb82 commit 7fd5a24

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

errors/invalid-getstaticprops-value.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Make sure to return the following shape from `getStaticProps`:
1212
export async function getStaticProps(ctx: {
1313
params?: ParsedUrlQuery
1414
preview?: boolean
15-
previewData?: any
15+
previewData?: PreviewData
1616
}) {
1717
return {
1818
props: { [key: string]: any }

packages/next/next-server/lib/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { NextRouter } from './router/router'
88
import { Env } from '@next/env'
99
import { BuildManifest } from '../server/get-page-files'
1010
import { DomainLocales } from '../server/config'
11+
import { PreviewData } from 'next/types'
1112

1213
/**
1314
* Types used by both next and next-server
@@ -220,7 +221,7 @@ export interface NextApiRequest extends IncomingMessage {
220221
/**
221222
* Preview data set on the request, if any
222223
* */
223-
previewData?: any
224+
previewData?: PreviewData
224225
}
225226

226227
/**

packages/next/next-server/server/api-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http'
22
import { parse } from 'next/dist/compiled/content-type'
33
import { CookieSerializeOptions } from 'next/dist/compiled/cookie'
44
import getRawBody from 'raw-body'
5-
import { PageConfig } from 'next/types'
5+
import { PageConfig, PreviewData } from 'next/types'
66
import { Stream } from 'stream'
77
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
88
import { decryptWithSecret, encryptWithSecret } from './crypto-utils'
@@ -292,7 +292,7 @@ export function tryGetPreviewData(
292292
req: IncomingMessage,
293293
res: ServerResponse,
294294
options: __ApiPreviewProps
295-
): object | string | false {
295+
): PreviewData {
296296
// Read cached preview data if present
297297
if (SYMBOL_PREVIEW_DATA in req) {
298298
return (req as any)[SYMBOL_PREVIEW_DATA] as any

packages/next/next-server/server/next-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import { detectDomainLocale } from '../lib/i18n/detect-domain-locale'
9191
import cookie from 'next/dist/compiled/cookie'
9292
import escapePathDelimiters from '../lib/router/utils/escape-path-delimiters'
9393
import { getUtils } from '../../build/webpack/loaders/next-serverless-loader/utils'
94+
import { PreviewData } from 'next/types'
9495

9596
const getCustomRouteMatcher = pathMatch(true)
9697

@@ -1460,7 +1461,7 @@ export default class Server {
14601461
const { i18n } = this.nextConfig
14611462
const locales = i18n?.locales
14621463

1463-
let previewData: string | false | object | undefined
1464+
let previewData: PreviewData
14641465
let isPreviewMode = false
14651466

14661467
if (hasServerProps || isSSG) {

packages/next/next-server/server/render.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
UNSTABLE_REVALIDATE_RENAME_ERROR,
1616
} from '../../lib/constants'
1717
import { isSerializableProps } from '../../lib/is-serializable-props'
18-
import { GetServerSideProps, GetStaticProps } from '../../types'
18+
import { GetServerSideProps, GetStaticProps, PreviewData } from '../../types'
1919
import { isInAmpMode } from '../lib/amp'
2020
import { AmpStateContext } from '../lib/amp-context'
2121
import {
@@ -559,7 +559,7 @@ export async function renderToHTML(
559559
await Loadable.preloadAll() // Make sure all dynamic imports are loaded
560560

561561
let isPreview
562-
let previewData: string | false | object | undefined
562+
let previewData: PreviewData
563563

564564
if ((isSSG || getServerSideProps) && !isFallback) {
565565
// Reads of this are cached on the `req` object, so this should resolve

packages/next/types/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ export {
9090
NextApiHandler,
9191
}
9292

93+
export type PreviewData = string | false | object | undefined
94+
9395
export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
9496
params?: Q
9597
preview?: boolean
96-
previewData?: any
98+
previewData?: PreviewData
9799
locale?: string
98100
locales?: string[]
99101
defaultLocale?: string
@@ -141,7 +143,7 @@ export type GetServerSidePropsContext<
141143
params?: Q
142144
query: ParsedUrlQuery
143145
preview?: boolean
144-
previewData?: any
146+
previewData?: PreviewData
145147
resolvedUrl: string
146148
locale?: string
147149
locales?: string[]

0 commit comments

Comments
 (0)