diff --git a/apps/nextjs/src/app/[locale]/boards/(content)/_creator.tsx b/apps/nextjs/src/app/[locale]/boards/(content)/_creator.tsx index 64c9289e32..106f1f562f 100644 --- a/apps/nextjs/src/app/[locale]/boards/(content)/_creator.tsx +++ b/apps/nextjs/src/app/[locale]/boards/(content)/_creator.tsx @@ -1,3 +1,4 @@ +import { cache } from "react"; import type { Metadata } from "next"; import { TRPCError } from "@trpc/server"; @@ -34,20 +35,21 @@ interface Props { export const createBoardContentPage = >({ getInitialBoardAsync: getInitialBoard, }: Props) => { + const getCachedBoard = cache((_paramsKey: string, params: TParams) => getInitialBoard(params)); + const getBoardForParams = (params: TParams) => getCachedBoard(JSON.stringify(params), params); + return { layout: createBoardLayout({ headerActions: , - getInitialBoardAsync: getInitialBoard, + getInitialBoardAsync: getBoardForParams, }), // eslint-disable-next-line no-restricted-syntax page: async ({ params }: { params: Promise }) => { - const session = await auth(); - const integrations = await getIntegrationsWithPermissionsAsync(session); - - const board = await getInitialBoard(await params); + const resolvedParams = await params; const queryClient = getQueryClient(); - // Prefetch item data + const [board, session] = await Promise.all([getBoardForParams(resolvedParams), auth()]); + const itemsMap = board.items.reduce((acc, item) => { const existing = acc.get(item.kind); if (existing) { @@ -58,17 +60,20 @@ export const createBoardContentPage = >( return acc; }, new Map()); - for (const [kind, items] of itemsMap) { - await prefetchForKindAsync(kind, queryClient, items).catch((error) => { - logger.error( - new ErrorWithMetadata( - "Failed to prefetch widget", - { widgetKind: kind, itemCount: items.length }, - { cause: error }, - ), - ); - }); - } + const [integrations] = await Promise.all([ + getIntegrationsWithPermissionsAsync(session), + ...Array.from(itemsMap).map(([kind, items]) => + prefetchForKindAsync(kind, queryClient, items).catch((error) => { + logger.error( + new ErrorWithMetadata( + "Failed to prefetch widget", + { widgetKind: kind, itemCount: items.length }, + { cause: error }, + ), + ); + }), + ), + ]); return ( @@ -80,7 +85,7 @@ export const createBoardContentPage = >( }, generateMetadataAsync: async ({ params }: { params: Promise }): Promise => { try { - const board = await getInitialBoard(await params); + const board = await getBoardForParams(await params); const t = await getI18n(); return { @@ -96,7 +101,6 @@ export const createBoardContentPage = >( }, }; } catch (error) { - // Ignore not found errors and return empty metadata if (error instanceof TRPCError && error.code === "NOT_FOUND") { return {}; }