Unverified Commit 7fd5a24d authored by tarunama's avatar tarunama Committed by GitHub

feat: add type to previewData (#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.
parent 41efb824
......@@ -12,7 +12,7 @@ Make sure to return the following shape from `getStaticProps`:
export async function getStaticProps(ctx: {
params?: ParsedUrlQuery
preview?: boolean
previewData?: any
previewData?: PreviewData
}) {
return {
props: { [key: string]: any }
......
......@@ -8,6 +8,7 @@ import { NextRouter } from './router/router'
import { Env } from '@next/env'
import { BuildManifest } from '../server/get-page-files'
import { DomainLocales } from '../server/config'
import { PreviewData } from 'next/types'
/**
* Types used by both next and next-server
......@@ -220,7 +221,7 @@ export interface NextApiRequest extends IncomingMessage {
/**
* Preview data set on the request, if any
* */
previewData?: any
previewData?: PreviewData
}
/**
......
......@@ -2,7 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { parse } from 'next/dist/compiled/content-type'
import { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import getRawBody from 'raw-body'
import { PageConfig } from 'next/types'
import { PageConfig, PreviewData } from 'next/types'
import { Stream } from 'stream'
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
import { decryptWithSecret, encryptWithSecret } from './crypto-utils'
......@@ -292,7 +292,7 @@ export function tryGetPreviewData(
req: IncomingMessage,
res: ServerResponse,
options: __ApiPreviewProps
): object | string | false {
): PreviewData {
// Read cached preview data if present
if (SYMBOL_PREVIEW_DATA in req) {
return (req as any)[SYMBOL_PREVIEW_DATA] as any
......
......@@ -91,6 +91,7 @@ import { detectDomainLocale } from '../lib/i18n/detect-domain-locale'
import cookie from 'next/dist/compiled/cookie'
import escapePathDelimiters from '../lib/router/utils/escape-path-delimiters'
import { getUtils } from '../../build/webpack/loaders/next-serverless-loader/utils'
import { PreviewData } from 'next/types'
const getCustomRouteMatcher = pathMatch(true)
......@@ -1460,7 +1461,7 @@ export default class Server {
const { i18n } = this.nextConfig
const locales = i18n?.locales
let previewData: string | false | object | undefined
let previewData: PreviewData
let isPreviewMode = false
if (hasServerProps || isSSG) {
......
......@@ -15,7 +15,7 @@ import {
UNSTABLE_REVALIDATE_RENAME_ERROR,
} from '../../lib/constants'
import { isSerializableProps } from '../../lib/is-serializable-props'
import { GetServerSideProps, GetStaticProps } from '../../types'
import { GetServerSideProps, GetStaticProps, PreviewData } from '../../types'
import { isInAmpMode } from '../lib/amp'
import { AmpStateContext } from '../lib/amp-context'
import {
......@@ -559,7 +559,7 @@ export async function renderToHTML(
await Loadable.preloadAll() // Make sure all dynamic imports are loaded
let isPreview
let previewData: string | false | object | undefined
let previewData: PreviewData
if ((isSSG || getServerSideProps) && !isFallback) {
// Reads of this are cached on the `req` object, so this should resolve
......
......@@ -90,10 +90,12 @@ export {
NextApiHandler,
}
export type PreviewData = string | false | object | undefined
export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
params?: Q
preview?: boolean
previewData?: any
previewData?: PreviewData
locale?: string
locales?: string[]
defaultLocale?: string
......@@ -141,7 +143,7 @@ export type GetServerSidePropsContext<
params?: Q
query: ParsedUrlQuery
preview?: boolean
previewData?: any
previewData?: PreviewData
resolvedUrl: string
locale?: string
locales?: string[]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment