-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
27 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
import type { GetStaticProps, GetStaticPropsContext } from "next"; | ||
import { unstable_cache } from "next/cache"; | ||
import { notFound, redirect } from "next/navigation"; | ||
|
||
export const withAppDirSsg = | ||
<T extends Record<string, any>>(getStaticProps: GetStaticProps<T>) => | ||
<T extends Record<string, any>>(getStaticProps: GetStaticProps<T>, routePath: string) => | ||
async (context: GetStaticPropsContext) => { | ||
const ssgResponse = await getStaticProps(context); | ||
const cacheKey = JSON.stringify({ | ||
route: routePath, | ||
params: context.params || {}, | ||
}); | ||
|
||
if ("redirect" in ssgResponse) { | ||
redirect(ssgResponse.redirect.destination); | ||
} | ||
const getCachedProps = unstable_cache(async () => { | ||
const ssgResponse = await getStaticProps(context); | ||
|
||
if ("notFound" in ssgResponse) { | ||
notFound(); | ||
} | ||
if ("redirect" in ssgResponse) { | ||
redirect(ssgResponse.redirect.destination); | ||
} | ||
|
||
const props = await Promise.resolve(ssgResponse.props); | ||
if ("notFound" in ssgResponse) { | ||
notFound(); | ||
} | ||
|
||
return { | ||
...ssgResponse.props, | ||
// includes dehydratedState required for future page trpcPropvider | ||
...("trpcState" in props && { dehydratedState: props.trpcState }), | ||
}; | ||
const props = await Promise.resolve(ssgResponse.props); | ||
|
||
return { | ||
...props, | ||
...("trpcState" in props && { dehydratedState: props.trpcState }), | ||
}; | ||
}, [`ssg-${cacheKey}`]); | ||
|
||
return getCachedProps(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters