chore: add cache to static pages in app router (#18278)

* add cache

* fix

* refactor
This commit is contained in:
Benny Joo
2024-12-20 13:37:20 -05:00
committed by GitHub
parent 6f1556a2fd
commit e04faffcbb
5 changed files with 25 additions and 18 deletions
+21 -14
View File
@@ -1,24 +1,31 @@
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 }),
};
return {
...ssgResponse.props,
...("trpcState" in ssgResponse.props && { dehydratedState: ssgResponse.props.trpcState }),
};
}, [`ssg-${cacheKey}`]);
return getCachedProps();
};
+1 -1
View File
@@ -13,7 +13,7 @@ import { buildLegacyCtx } from "@lib/buildLegacyCtx";
import type { PageProps } from "~/apps/[slug]/slug-view";
import Page from "~/apps/[slug]/slug-view";
const getData = withAppDirSsg<PageProps>(getStaticProps);
const getData = withAppDirSsg<PageProps>(getStaticProps, "future/apps/[slug]");
export const generateMetadata = async ({ params, searchParams }: _PageProps) => {
const legacyContext = buildLegacyCtx(headers(), cookies(), params, searchParams);
@@ -28,7 +28,7 @@ export const generateStaticParams = async () => {
return paths.map((category) => ({ category }));
};
const getData = withAppDirSsg<PageProps>(getStaticProps);
const getData = withAppDirSsg<PageProps>(getStaticProps, "future/apps/categories/[category]");
export default WithLayout({ getData, Page: CategoryPage, getLayout: null })<"P">;
export const dynamic = "force-static";
+1 -1
View File
@@ -13,7 +13,7 @@ export const generateMetadata = async () => {
);
};
const getData = withAppDirSsg(getStaticProps);
const getData = withAppDirSsg(getStaticProps, "future/auth/error");
export default WithLayout({ getData, Page, getLayout: null })<"P">;
export const dynamic = "force-static";
@@ -8,7 +8,7 @@ import Page from "~/bookings/views/bookings-listing-view";
import { getStaticProps } from "~/bookings/views/bookings-listing-view.getStaticProps";
type Y = InferGetStaticPropsType<typeof getStaticProps>;
const getData = withAppDirSsg<Y>(getStaticProps);
const getData = withAppDirSsg<Y>(getStaticProps, "future/bookings/[status]");
export const generateMetadata = async () =>
await _generateMetadata(