diff --git a/apps/next-app/pages/modules/[questionBank]/flashcards/[collectionId]/index.page.tsx b/apps/next-app/pages/modules/[questionBank]/flashcards/[collectionId]/index.page.tsx index cc35fbd76..5a880615e 100644 --- a/apps/next-app/pages/modules/[questionBank]/flashcards/[collectionId]/index.page.tsx +++ b/apps/next-app/pages/modules/[questionBank]/flashcards/[collectionId]/index.page.tsx @@ -1,11 +1,10 @@ import * as fs from "node:fs/promises"; -import { getTrpcHelper } from "libs/trpc/server/src/next/trpc-helper"; import { AppHead } from "@chair-flight/react/components"; import { FlashcardList, LayoutModule } from "@chair-flight/react/containers"; -import { staticHandler } from "@chair-flight/trpc/server"; +import { staticHandler, staticPathsHandler } from "@chair-flight/trpc/server"; import type { QuestionBankName } from "@chair-flight/base/types"; import type { Breadcrumbs } from "@chair-flight/react/containers"; -import type { GetStaticPaths, NextPage } from "next"; +import type { NextPage } from "next"; type PageProps = { questionBank: QuestionBankName; @@ -41,23 +40,26 @@ export const getStaticProps = staticHandler( fs, ); -export const getStaticPaths: GetStaticPaths = async () => { - const helper = await getTrpcHelper(); - const qb = helper.questionBank; - const banks: QuestionBankName[] = ["prep"]; - const paths = await Promise.all( - banks.map(async (questionBank) => { - const params = { questionBank }; - const data = await qb.getFlashcardsCollections.fetch(params); - return data.collections.map(({ id: collectionId }) => ({ - params: { - questionBank, - collectionId, - }, - })); - }), - ).then((c) => c.flat()); - return { fallback: false, paths }; -}; +export const getStaticPaths = staticPathsHandler( + async ({ helper }) => { + const qb = helper.questionBank; + const banks: QuestionBankName[] = ["prep"]; + const paths = await Promise.all( + banks.map(async (questionBank) => { + const params = { questionBank }; + const data = await qb.getFlashcardsCollections.fetch(params); + return data.collections.map(({ id: collectionId }) => ({ + params: { + questionBank, + collectionId, + }, + })); + }), + ).then((c) => c.flat()); + + return { fallback: false, paths }; + }, + fs, +); export default Page; diff --git a/libs/core/question-bank/src/question-bank.ts b/libs/core/question-bank/src/question-bank.ts index 5d15174cc..1ab4c51ad 100644 --- a/libs/core/question-bank/src/question-bank.ts +++ b/libs/core/question-bank/src/question-bank.ts @@ -83,7 +83,6 @@ export class QuestionBank implements IQuestionBank { const bankPath = `/content/content-question-bank-${this.getName()}`; const baseApiPath = `${urlPath}${bankPath}`; const apiPath = `${baseApiPath}/${resource}.json`; - console.log("apiPath", apiPath); const response = await fetch(apiPath); const json = (await response.json()) as NameToType[T][]; type ArrayType = (typeof this.resourceArrays)[typeof resource]; diff --git a/libs/trpc/server/src/index.ts b/libs/trpc/server/src/index.ts index 1406fa13f..ce19bdff7 100644 --- a/libs/trpc/server/src/index.ts +++ b/libs/trpc/server/src/index.ts @@ -1,5 +1,5 @@ export { edgeApiHandler, apiHandler } from "./next/api-handler"; export { ssrHandler } from "./next/ssr-handler"; -export { staticHandler } from "./next/static-handler"; +export { staticHandler, staticPathsHandler } from "./next/static-handler"; export type { TrpcHelper } from "./next/trpc-helper"; export type { AppRouter } from "./config/app-router"; diff --git a/libs/trpc/server/src/next/static-handler.ts b/libs/trpc/server/src/next/static-handler.ts index 83e0b2031..95dc1ab74 100644 --- a/libs/trpc/server/src/next/static-handler.ts +++ b/libs/trpc/server/src/next/static-handler.ts @@ -2,6 +2,9 @@ import { questionBanks } from "@chair-flight/core/question-bank"; import { getTrpcHelper } from "./trpc-helper"; import type { TrpcHelper } from "./trpc-helper"; import type { + GetStaticPaths, + GetStaticPathsContext, + GetStaticPathsResult, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,3 +53,24 @@ export const staticHandler = < return response; }; }; + +export const staticPathsHandler = < + Params extends ParsedUrlQuery = ParsedUrlQuery, +>( + handler: ({ + context, + helper, + }: { + context: GetStaticPathsContext; + helper: TrpcHelper; + }) => Promise>, + fs: FS, +): GetStaticPaths => { + return async (context) => { + await Promise.all( + Object.values(questionBanks).map((qb) => qb.preloadForStaticRender(fs)), + ); + const helper = await getTrpcHelper(); + return await handler({ context, helper }); + }; +};