From 67fa74a0e3c7f71d9d6f9e769659c3959513941a Mon Sep 17 00:00:00 2001 From: Blake Wilson Date: Thu, 1 Jul 2021 12:49:41 -0500 Subject: [PATCH] feat: (#321) support `redirect` and `notFound` --- packages/next/src/getProps.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/next/src/getProps.ts b/packages/next/src/getProps.ts index 91a7e24a7..6d8743b7a 100644 --- a/packages/next/src/getProps.ts +++ b/packages/next/src/getProps.ts @@ -1,12 +1,13 @@ /* eslint-disable react/no-children-prop */ import { CategoryIdType, PageIdType, PostIdType } from '@faustjs/core'; -import { isNumber, isObject } from 'lodash'; +import { isBoolean, isNumber, isObject } from 'lodash'; import isNil from 'lodash/isNil'; import { GetServerSidePropsContext, GetStaticPropsContext, GetStaticPropsResult, GetServerSidePropsResult, + Redirect, } from 'next'; import { RouterContext } from 'next/dist/next-server/lib/router-context'; @@ -29,6 +30,8 @@ export interface GetNextServerSidePropsConfig> { client: ReturnType; Page?: FunctionComponent | ComponentClass; props?: Props; + notFound?: boolean; + redirect?: Redirect; } export interface GetNextStaticPropsConfig> { @@ -36,6 +39,8 @@ export interface GetNextStaticPropsConfig> { Page?: FunctionComponent | ComponentClass; props?: Props; revalidate?: number | boolean; + notFound?: boolean; + redirect?: Redirect; } export interface PageProps { @@ -194,6 +199,20 @@ export async function getNextServerSideProps( context: GetServerSidePropsContext, config: GetNextServerSidePropsConfig, ): Promise> { + const { notFound, redirect } = config; + + if (isBoolean(notFound) && notFound === true) { + return { + notFound, + }; + } + + if (isObject(redirect)) { + return { + redirect, + }; + } + return getProps(context, config); } @@ -201,6 +220,20 @@ export async function getNextStaticProps( context: GetStaticPropsContext, config: GetNextStaticPropsConfig, ): Promise> { + const { notFound, redirect } = config; + + if (isBoolean(notFound) && notFound === true) { + return { + notFound, + }; + } + + if (isObject(redirect)) { + return { + redirect, + }; + } + const pageProps: GetStaticPropsResult = await getProps( context, config,