Skip to content

Commit

Permalink
add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Dec 19, 2024
1 parent 5475638 commit f485626
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
37 changes: 23 additions & 14 deletions apps/web/app/WithAppDirSsg.tsx
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();
};
2 changes: 1 addition & 1 deletion apps/web/app/bookings/[status]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import BookingsListingView 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, "bookings/[status]");

export const generateMetadata = async () =>
await _generateMetadata(
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/future/apps/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/future/apps/categories/[category]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
2 changes: 1 addition & 1 deletion apps/web/app/future/auth/error/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit f485626

Please sign in to comment.