Unverified Commit a8088bfd authored by Pablo Espinosa's avatar Pablo Espinosa Committed by GitHub

feat: Adding generic typing for previewData (#28668)

on GetServerSidePropsContext and GetStaticPropsContext

As discussed on #21574 having a generic type will give it more flexibility and remove linting errors.

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using #21574 
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`
parent 1b8bc249
...@@ -94,10 +94,13 @@ export { ...@@ -94,10 +94,13 @@ export {
export type PreviewData = string | false | object | undefined export type PreviewData = string | false | object | undefined
export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = { export type GetStaticPropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = {
params?: Q params?: Q
preview?: boolean preview?: boolean
previewData?: PreviewData previewData?: D
locale?: string locale?: string
locales?: string[] locales?: string[]
defaultLocale?: string defaultLocale?: string
...@@ -110,9 +113,10 @@ export type GetStaticPropsResult<P> = ...@@ -110,9 +113,10 @@ export type GetStaticPropsResult<P> =
export type GetStaticProps< export type GetStaticProps<
P extends { [key: string]: any } = { [key: string]: any }, P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = ( > = (
context: GetStaticPropsContext<Q> context: GetStaticPropsContext<Q, D>
) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P> ) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>
export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any> export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
...@@ -138,7 +142,8 @@ export type GetStaticPaths<P extends ParsedUrlQuery = ParsedUrlQuery> = ( ...@@ -138,7 +142,8 @@ export type GetStaticPaths<P extends ParsedUrlQuery = ParsedUrlQuery> = (
) => Promise<GetStaticPathsResult<P>> | GetStaticPathsResult<P> ) => Promise<GetStaticPathsResult<P>> | GetStaticPathsResult<P>
export type GetServerSidePropsContext< export type GetServerSidePropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = { > = {
req: IncomingMessage & { req: IncomingMessage & {
cookies: NextApiRequestCookies cookies: NextApiRequestCookies
...@@ -147,7 +152,7 @@ export type GetServerSidePropsContext< ...@@ -147,7 +152,7 @@ export type GetServerSidePropsContext<
params?: Q params?: Q
query: ParsedUrlQuery query: ParsedUrlQuery
preview?: boolean preview?: boolean
previewData?: PreviewData previewData?: D
resolvedUrl: string resolvedUrl: string
locale?: string locale?: string
locales?: string[] locales?: string[]
...@@ -161,9 +166,10 @@ export type GetServerSidePropsResult<P> = ...@@ -161,9 +166,10 @@ export type GetServerSidePropsResult<P> =
export type GetServerSideProps< export type GetServerSideProps<
P extends { [key: string]: any } = { [key: string]: any }, P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = ( > = (
context: GetServerSidePropsContext<Q> context: GetServerSidePropsContext<Q, D>
) => Promise<GetServerSidePropsResult<P>> ) => Promise<GetServerSidePropsResult<P>>
export type InferGetServerSidePropsType<T> = T extends GetServerSideProps< export type InferGetServerSidePropsType<T> = T extends GetServerSideProps<
......
...@@ -6,6 +6,11 @@ type Params = { ...@@ -6,6 +6,11 @@ type Params = {
type Props = { type Props = {
data: string data: string
title: string
}
type PreviewData = {
title: string
} }
export const getStaticPaths: GetStaticPaths<Params> = async () => { export const getStaticPaths: GetStaticPaths<Params> = async () => {
...@@ -15,15 +20,21 @@ export const getStaticPaths: GetStaticPaths<Params> = async () => { ...@@ -15,15 +20,21 @@ export const getStaticPaths: GetStaticPaths<Params> = async () => {
} }
} }
export const getStaticProps: GetStaticProps<Props, Params> = async ({ export const getStaticProps: GetStaticProps<Props, Params, PreviewData> =
params, async ({ params, previewData }) => {
}) => { return {
return { props: {
props: { data: params!.slug }, data: params!.slug,
revalidate: false, title: previewData?.title || 'default title',
},
revalidate: false,
}
} }
}
export default function Page({ data }: Props) { export default function Page({ data, title }: Props) {
return <h1>{data}</h1> return (
<h1>
{data} {title}
</h1>
)
} }
...@@ -6,16 +6,27 @@ type Params = { ...@@ -6,16 +6,27 @@ type Params = {
type Props = { type Props = {
data: string data: string
title: string
} }
export const getServerSideProps: GetServerSideProps<Props, Params> = async ({ type PreviewData = {
params, title: string
}) => { }
export const getServerSideProps: GetServerSideProps<
Props,
Params,
PreviewData
> = async ({ params, previewData }) => {
return { return {
props: { data: params!.slug }, props: { data: params!.slug, title: previewData?.title || 'default title' },
} }
} }
export default function Page({ data }: Props) { export default function Page({ data, title }: Props) {
return <h1>{data}</h1> return (
<h1>
{data} {title}
</h1>
)
} }
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