Skip to content

Commit

Permalink
Refactor getNext... and is404 config (#329)
Browse files Browse the repository at this point in the history
* feat: (#321) Support revalidate config option

* feat: (#321) support `redirect` and `notFound`

* refactor: (#321) destructure revalidate

* test: (#321) create tests for getNextServerSideProps and getNextStaticProps

* refactor: (#324) Update is404 param structure

* docs: (#324) update is404 docs

* refactor: (#324) use `notFound` config key for is404
  • Loading branch information
blakewilson authored Jul 1, 2021
1 parent a532b76 commit 55c22f1
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 33 deletions.
9 changes: 2 additions & 7 deletions examples/next/getting-started/src/pages/[...pageUri].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getNextStaticProps, is404 } from '@faustjs/next';
import { Footer, Header, Hero } from 'components';
import { GetStaticPropsContext } from 'next';
import Head from 'next/head';
import { client, Page as PageType } from 'client';
import { client, Page as PageType } from 'client';

export interface PageProps {
page: PageType | PageType['preview']['node'] | null | undefined;
Expand Down Expand Up @@ -49,15 +49,10 @@ export default function Page() {
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
notFound: await is404(context, { client }),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ export default function Page() {
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
notFound: await is404(context, { client }),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,10 @@ export default function Page() {
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
notFound: await is404(context, { client }),
});
}

Expand Down
9 changes: 2 additions & 7 deletions internal/website/docs/next/reference/handle-404s.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,10 @@ export default function Page() {
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
client,
notFound: await is404(context, { client }),
});
}
```
Expand Down
62 changes: 55 additions & 7 deletions 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 { 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 @@ -25,22 +26,41 @@ import {

export const CLIENT_CACHE_PROP = '__CLIENT_CACHE_PROP';

export interface NextPropsConfig<Props = Record<string, unknown>> {
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> {
props: Props & { [CLIENT_CACHE_PROP]: string | null };
}

export interface Is404Config {
client: ReturnType<typeof getClient>;
}

export async function getProps<
Context extends GetStaticPropsContext | GetServerSidePropsContext,
Props,
>(
context: Context,
{ client, Page, props }: NextPropsConfig,
{
client,
Page,
props,
}: GetNextServerSidePropsConfig | GetNextStaticPropsConfig,
): Promise<PageProps<Props>> {
let cacheSnapshot: string | undefined;
client.setAsRoot();
Expand Down Expand Up @@ -83,7 +103,7 @@ export async function getProps<

export async function is404<
Context extends GetStaticPropsContext | GetServerSidePropsContext,
>(client: ReturnType<typeof getClient>, { params }: Context): Promise<boolean> {
>({ params }: Context, { client }: Is404Config): Promise<boolean> {
if (!params) {
return false;
}
Expand Down Expand Up @@ -181,23 +201,51 @@ export async function is404<

export async function getNextServerSideProps<Props>(
context: GetServerSidePropsContext,
config: NextPropsConfig,
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: NextPropsConfig,
config: GetNextStaticPropsConfig,
): Promise<GetStaticPropsResult<Props>> {
const { notFound, redirect, revalidate } = config;

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

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

const pageProps: GetStaticPropsResult<Props> = await getProps(
context,
config,
);

/* eslint-disable @typescript-eslint/no-explicit-any */
if (isObject(pageProps.props)) {
pageProps.revalidate = 1;
pageProps.revalidate = isNumber(revalidate) ? revalidate : 1;
}
/* eslint-enable @typescript-eslint/no-explicit-any */

Expand Down
Loading

0 comments on commit 55c22f1

Please sign in to comment.