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 {
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
preview?: boolean
previewData?: PreviewData
previewData?: D
locale?: string
locales?: string[]
defaultLocale?: string
......@@ -110,9 +113,10 @@ export type GetStaticPropsResult<P> =
export type GetStaticProps<
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>
export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
......@@ -138,7 +142,8 @@ export type GetStaticPaths<P extends ParsedUrlQuery = ParsedUrlQuery> = (
) => Promise<GetStaticPathsResult<P>> | GetStaticPathsResult<P>
export type GetServerSidePropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery
Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = {
req: IncomingMessage & {
cookies: NextApiRequestCookies
......@@ -147,7 +152,7 @@ export type GetServerSidePropsContext<
params?: Q
query: ParsedUrlQuery
preview?: boolean
previewData?: PreviewData
previewData?: D
resolvedUrl: string
locale?: string
locales?: string[]
......@@ -161,9 +166,10 @@ export type GetServerSidePropsResult<P> =
export type GetServerSideProps<
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>>
export type InferGetServerSidePropsType<T> = T extends GetServerSideProps<
......
......@@ -6,6 +6,11 @@ type Params = {
type Props = {
data: string
title: string
}
type PreviewData = {
title: string
}
export const getStaticPaths: GetStaticPaths<Params> = async () => {
......@@ -15,15 +20,21 @@ export const getStaticPaths: GetStaticPaths<Params> = async () => {
}
}
export const getStaticProps: GetStaticProps<Props, Params> = async ({
params,
}) => {
return {
props: { data: params!.slug },
revalidate: false,
export const getStaticProps: GetStaticProps<Props, Params, PreviewData> =
async ({ params, previewData }) => {
return {
props: {
data: params!.slug,
title: previewData?.title || 'default title',
},
revalidate: false,
}
}
}
export default function Page({ data }: Props) {
return <h1>{data}</h1>
export default function Page({ data, title }: Props) {
return (
<h1>
{data} {title}
</h1>
)
}
......@@ -6,16 +6,27 @@ type Params = {
type Props = {
data: string
title: string
}
export const getServerSideProps: GetServerSideProps<Props, Params> = async ({
params,
}) => {
type PreviewData = {
title: string
}
export const getServerSideProps: GetServerSideProps<
Props,
Params,
PreviewData
> = async ({ params, previewData }) => {
return {
props: { data: params!.slug },
props: { data: params!.slug, title: previewData?.title || 'default title' },
}
}
export default function Page({ data }: Props) {
return <h1>{data}</h1>
export default function Page({ data, title }: Props) {
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