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