Skip to content

Commit

Permalink
feat: (#321) support redirect and notFound
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewilson committed Jul 1, 2021
1 parent 5748de7 commit 67fa74a
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/next/src/getProps.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -29,13 +30,17 @@ export interface GetNextServerSidePropsConfig<Props = Record<string, unknown>> {
client: ReturnType<typeof getClient>;
Page?: FunctionComponent | ComponentClass;
props?: Props;
notFound?: boolean;
redirect?: Redirect;
}

export interface GetNextStaticPropsConfig<Props = Record<string, unknown>> {
client: ReturnType<typeof getClient>;
Page?: FunctionComponent | ComponentClass;
props?: Props;
revalidate?: number | boolean;
notFound?: boolean;
redirect?: Redirect;
}

export interface PageProps<Props> {
Expand Down Expand Up @@ -194,13 +199,41 @@ export async function getNextServerSideProps<Props>(
context: GetServerSidePropsContext,
config: GetNextServerSidePropsConfig,
): Promise<GetServerSidePropsResult<Props>> {
const { notFound, redirect } = config;

if (isBoolean(notFound) && notFound === true) {
return {
notFound,
};
}

if (isObject(redirect)) {
return {
redirect,
};
}

return getProps(context, config);
}

export async function getNextStaticProps<Props>(
context: GetStaticPropsContext,
config: GetNextStaticPropsConfig,
): Promise<GetStaticPropsResult<Props>> {
const { notFound, redirect } = config;

if (isBoolean(notFound) && notFound === true) {
return {
notFound,
};
}

if (isObject(redirect)) {
return {
redirect,
};
}

const pageProps: GetStaticPropsResult<Props> = await getProps(
context,
config,
Expand Down

0 comments on commit 67fa74a

Please sign in to comment.