diff --git a/apps/code/src/app/dashboard/feedback/dev-plan-cancellation/page.tsx b/apps/code/src/app/dashboard/feedback/dev-plan-cancellation/page.tsx index 082c4b7a9f..bb3f0ff184 100644 --- a/apps/code/src/app/dashboard/feedback/dev-plan-cancellation/page.tsx +++ b/apps/code/src/app/dashboard/feedback/dev-plan-cancellation/page.tsx @@ -17,19 +17,22 @@ interface EligibilityResponse { } export default async function DevPlanCancellationFeedbackPage() { - const userData = await fetchServerData<{ user: { id: string } } | null>( + const userDataPromise = fetchServerData<{ user: { id: string } } | null>( "GET", "/user/me", ); + const eligibilityPromise = fetchServerData( + "GET", + "/dev-plan-cancellation-feedback/eligibility", + ); + + const userData = await userDataPromise; if (!userData?.user) { redirect("/login?returnUrl=/dashboard/feedback/dev-plan-cancellation"); } - const eligibility = await fetchServerData( - "GET", - "/dev-plan-cancellation-feedback/eligibility", - ); + const eligibility = await eligibilityPromise; return (
diff --git a/apps/code/src/app/dashboard/survey/page.tsx b/apps/code/src/app/dashboard/survey/page.tsx index 8d9f68c62d..f63692698c 100644 --- a/apps/code/src/app/dashboard/survey/page.tsx +++ b/apps/code/src/app/dashboard/survey/page.tsx @@ -13,19 +13,22 @@ export const metadata = { }; export default async function SurveyPage() { - const userData = await fetchServerData<{ user: { id: string } } | null>( + const userDataPromise = fetchServerData<{ user: { id: string } } | null>( "GET", "/user/me", ); + const eligibilityPromise = fetchServerData( + "GET", + "/model-survey/eligibility", + ); + + const userData = await userDataPromise; if (!userData?.user) { redirect("/login?returnUrl=/dashboard/survey"); } - const eligibility = await fetchServerData( - "GET", - "/model-survey/eligibility", - ); + const eligibility = await eligibilityPromise; if (!eligibility || eligibility.topModels.length === 0) { const year = eligibility?.year ?? new Date().getUTCFullYear(); diff --git a/apps/playground/src/app/audio/page.tsx b/apps/playground/src/app/audio/page.tsx index 31a900740d..5324c5bb08 100644 --- a/apps/playground/src/app/audio/page.tsx +++ b/apps/playground/src/app/audio/page.tsx @@ -32,36 +32,31 @@ export default async function AudioPage({ cookieStore.get(AUDIO_MODEL_COOKIE)?.value, ); - const [models, providers] = await Promise.all([ - fetchModels(), - fetchProviders(), - ]); - - // Ensure the dedicated Chat org exists, then list it so it can back the - // default billing context for the playground. - await fetchServerData("GET", "/playground/chat-org"); - const initialOrganizationsData = await fetchServerData("GET", "/orgs", { - params: { query: { includeChat: "true" } }, - }); - - let initialProjectsData: { projects: Project[] } | null = null; - if (orgId) { - try { - initialProjectsData = (await fetchServerData( - "GET", - "/orgs/{id}/projects", - { - params: { - path: { - id: orgId, + const [models, providers, initialOrganizationsData, orgIdProjectsData] = + await Promise.all([ + fetchModels(), + fetchProviders(), + // Ensure the dedicated Chat org exists, then list it so it can back the + // default billing context for the playground. + fetchServerData("GET", "/playground/chat-org").then(() => + fetchServerData("GET", "/orgs", { + params: { query: { includeChat: "true" } }, + }), + ), + orgId + ? fetchServerData("GET", "/orgs/{id}/projects", { + params: { + path: { + id: orgId, + }, }, - }, - }, - )) as { projects: Project[] }; - } catch (error) { - console.warn("Failed to fetch projects for organization:", orgId, error); - } - } + }) + : null, + ]); + + let initialProjectsData = (orgIdProjectsData ?? null) as { + projects: Project[]; + } | null; if ( projectId && diff --git a/apps/playground/src/app/canvas/page.tsx b/apps/playground/src/app/canvas/page.tsx index a37fbd2b55..d2eedac051 100644 --- a/apps/playground/src/app/canvas/page.tsx +++ b/apps/playground/src/app/canvas/page.tsx @@ -32,36 +32,31 @@ export default async function CanvasPage({ cookieStore.get(CANVAS_MODEL_COOKIE)?.value, ); - const [models, providers] = await Promise.all([ - fetchModels(), - fetchProviders(), - ]); - - // Ensure the dedicated Chat org exists, then list it so it can back the - // default billing context for the playground. - await fetchServerData("GET", "/playground/chat-org"); - const initialOrganizationsData = await fetchServerData("GET", "/orgs", { - params: { query: { includeChat: "true" } }, - }); - - let initialProjectsData: { projects: Project[] } | null = null; - if (orgId) { - try { - initialProjectsData = (await fetchServerData( - "GET", - "/orgs/{id}/projects", - { - params: { - path: { - id: orgId, + const [models, providers, initialOrganizationsData, orgIdProjectsData] = + await Promise.all([ + fetchModels(), + fetchProviders(), + // Ensure the dedicated Chat org exists, then list it so it can back the + // default billing context for the playground. + fetchServerData("GET", "/playground/chat-org").then(() => + fetchServerData("GET", "/orgs", { + params: { query: { includeChat: "true" } }, + }), + ), + orgId + ? fetchServerData("GET", "/orgs/{id}/projects", { + params: { + path: { + id: orgId, + }, }, - }, - }, - )) as { projects: Project[] }; - } catch (error) { - console.warn("Failed to fetch projects for organization:", orgId, error); - } - } + }) + : null, + ]); + + let initialProjectsData = (orgIdProjectsData ?? null) as { + projects: Project[]; + } | null; const allOrganizations = ( initialOrganizationsData && diff --git a/apps/playground/src/app/image/page.tsx b/apps/playground/src/app/image/page.tsx index 04da0129d9..3df5f8c499 100644 --- a/apps/playground/src/app/image/page.tsx +++ b/apps/playground/src/app/image/page.tsx @@ -32,36 +32,31 @@ export default async function ImagePage({ cookieStore.get(IMAGE_MODEL_COOKIE)?.value, ); - const [models, providers] = await Promise.all([ - fetchModels(), - fetchProviders(), - ]); - - // Ensure the dedicated Chat org exists, then list it so it can back the - // default billing context for the playground. - await fetchServerData("GET", "/playground/chat-org"); - const initialOrganizationsData = await fetchServerData("GET", "/orgs", { - params: { query: { includeChat: "true" } }, - }); - - let initialProjectsData: { projects: Project[] } | null = null; - if (orgId) { - try { - initialProjectsData = (await fetchServerData( - "GET", - "/orgs/{id}/projects", - { - params: { - path: { - id: orgId, + const [models, providers, initialOrganizationsData, orgIdProjectsData] = + await Promise.all([ + fetchModels(), + fetchProviders(), + // Ensure the dedicated Chat org exists, then list it so it can back the + // default billing context for the playground. + fetchServerData("GET", "/playground/chat-org").then(() => + fetchServerData("GET", "/orgs", { + params: { query: { includeChat: "true" } }, + }), + ), + orgId + ? fetchServerData("GET", "/orgs/{id}/projects", { + params: { + path: { + id: orgId, + }, }, - }, - }, - )) as { projects: Project[] }; - } catch (error) { - console.warn("Failed to fetch projects for organization:", orgId, error); - } - } + }) + : null, + ]); + + let initialProjectsData = (orgIdProjectsData ?? null) as { + projects: Project[]; + } | null; if ( projectId && diff --git a/apps/playground/src/app/playground-shell.tsx b/apps/playground/src/app/playground-shell.tsx index 976457ff16..e136ca2d64 100644 --- a/apps/playground/src/app/playground-shell.tsx +++ b/apps/playground/src/app/playground-shell.tsx @@ -65,6 +65,11 @@ export async function renderPlaygroundShell({ redirect(`/?${newParams.toString()}`); } + // Start the model catalogue fetches immediately — they don't depend on any + // of the org/billing lookups below, so they resolve while those run. + const modelsPromise = fetchModels(); + const providersPromise = fetchProviders(); + const initialOrganizationsData = await fetchServerData("GET", "/orgs"); const allOrganizations = ( initialOrganizationsData && @@ -201,8 +206,8 @@ export async function renderPlaygroundShell({ const projects = (initialProjectsData?.projects ?? []) as Project[]; const [models, providers] = await Promise.all([ - fetchModels(), - fetchProviders(), + modelsPromise, + providersPromise, ]); let selectedProject: Project | null = null; diff --git a/apps/playground/src/app/realtime/page.tsx b/apps/playground/src/app/realtime/page.tsx index db4e7d8995..47adbcdb5d 100644 --- a/apps/playground/src/app/realtime/page.tsx +++ b/apps/playground/src/app/realtime/page.tsx @@ -31,13 +31,12 @@ export default async function RealtimePage({ cookieStore.get(REALTIME_MODEL_COOKIE)?.value, ); - const [models, providers] = await Promise.all([ + const [models, providers, initialOrganizationsData] = await Promise.all([ fetchModels(), fetchProviders(), + fetchServerData("GET", "/orgs"), ]); - const initialOrganizationsData = await fetchServerData("GET", "/orgs"); - const allOrganizations = ( initialOrganizationsData && typeof initialOrganizationsData === "object" && diff --git a/apps/playground/src/app/video/page.tsx b/apps/playground/src/app/video/page.tsx index 76b4f4fbd7..38cbba57bc 100644 --- a/apps/playground/src/app/video/page.tsx +++ b/apps/playground/src/app/video/page.tsx @@ -32,36 +32,31 @@ export default async function VideoPage({ cookieStore.get(VIDEO_MODEL_COOKIE)?.value, ); - const [models, providers] = await Promise.all([ - fetchModels(), - fetchProviders(), - ]); - - // Ensure the dedicated Chat org exists, then list it so it can back the - // default billing context for the playground. - await fetchServerData("GET", "/playground/chat-org"); - const initialOrganizationsData = await fetchServerData("GET", "/orgs", { - params: { query: { includeChat: "true" } }, - }); - - let initialProjectsData: { projects: Project[] } | null = null; - if (orgId) { - try { - initialProjectsData = (await fetchServerData( - "GET", - "/orgs/{id}/projects", - { - params: { - path: { - id: orgId, + const [models, providers, initialOrganizationsData, orgIdProjectsData] = + await Promise.all([ + fetchModels(), + fetchProviders(), + // Ensure the dedicated Chat org exists, then list it so it can back the + // default billing context for the playground. + fetchServerData("GET", "/playground/chat-org").then(() => + fetchServerData("GET", "/orgs", { + params: { query: { includeChat: "true" } }, + }), + ), + orgId + ? fetchServerData("GET", "/orgs/{id}/projects", { + params: { + path: { + id: orgId, + }, }, - }, - }, - )) as { projects: Project[] }; - } catch (error) { - console.warn("Failed to fetch projects for organization:", orgId, error); - } - } + }) + : null, + ]); + + let initialProjectsData = (orgIdProjectsData ?? null) as { + projects: Project[]; + } | null; if ( projectId && diff --git a/apps/ui/src/app/dashboard/[orgId]/[projectId]/page.tsx b/apps/ui/src/app/dashboard/[orgId]/[projectId]/page.tsx index 7b397312f5..7cc5969f34 100644 --- a/apps/ui/src/app/dashboard/[orgId]/[projectId]/page.tsx +++ b/apps/ui/src/app/dashboard/[orgId]/[projectId]/page.tsx @@ -18,17 +18,6 @@ export default async function Dashboard({ }>; }) { const { orgId, projectId } = await params; - - // Project-scoped "developer" members don't get the project-wide dashboard — - // send them to their personal usage view. - const orgsData = await fetchServerData<{ - organizations?: { id: string; role?: string }[]; - }>("GET", "/orgs"); - const role = orgsData?.organizations?.find((o) => o.id === orgId)?.role; - if (role === "developer") { - redirect(`/dashboard/${orgId}/${projectId}/me`); - } - const searchParamsData = searchParams ? await searchParams : {}; const today = new Date(); @@ -36,7 +25,11 @@ export default async function Dashboard({ searchParamsData?.from ?? format(subDays(today, 6), "yyyy-MM-dd"); const toParam = searchParamsData?.to ?? format(today, "yyyy-MM-dd"); - const initialActivityData = await fetchServerData( + const orgsDataPromise = fetchServerData<{ + organizations?: { id: string; role?: string }[]; + }>("GET", "/orgs"); + + const initialActivityDataPromise = fetchServerData( "GET", "/activity", { @@ -50,6 +43,16 @@ export default async function Dashboard({ }, ); + // Project-scoped "developer" members don't get the project-wide dashboard — + // send them to their personal usage view. + const orgsData = await orgsDataPromise; + const role = orgsData?.organizations?.find((o) => o.id === orgId)?.role; + if (role === "developer") { + redirect(`/dashboard/${orgId}/${projectId}/me`); + } + + const initialActivityData = await initialActivityDataPromise; + return ( ); diff --git a/apps/ui/src/app/dashboard/[orgId]/layout.tsx b/apps/ui/src/app/dashboard/[orgId]/layout.tsx index 91059ec13f..78cd60b9f9 100644 --- a/apps/ui/src/app/dashboard/[orgId]/layout.tsx +++ b/apps/ui/src/app/dashboard/[orgId]/layout.tsx @@ -18,14 +18,29 @@ interface OrgLayoutProps { export default async function OrgLayout({ children, params }: OrgLayoutProps) { const { orgId } = await params; - const initialUserData = await fetchServerData< + const initialUserDataPromise = fetchServerData< { user: User } | undefined | null >("GET", "/user/me"); - const initialOrganizationsData = await fetchServerData<{ + const initialOrganizationsDataPromise = fetchServerData<{ organizations: Organization[]; }>("GET", "/orgs"); + const initialProjectsDataPromise = orgId + ? fetchServerData("GET", "/orgs/{id}/projects", { + params: { + path: { + id: orgId, + }, + }, + }) + : null; + + const [initialUserData, initialOrganizationsData] = await Promise.all([ + initialUserDataPromise, + initialOrganizationsDataPromise, + ]); + const orgs = initialOrganizationsData?.organizations ?? []; const isAuthorizedForOrg = orgs.some((org) => org.id === orgId); @@ -42,17 +57,7 @@ export default async function OrgLayout({ children, params }: OrgLayoutProps) { if (orgId) { try { - initialProjectsData = await fetchServerData( - "GET", - "/orgs/{id}/projects", - { - params: { - path: { - id: orgId, - }, - }, - }, - ); + initialProjectsData = await initialProjectsDataPromise; // Get last used project for navigation fallback if ( diff --git a/apps/ui/src/app/dashboard/page.tsx b/apps/ui/src/app/dashboard/page.tsx index 3d54a7bb0d..d614b8ec02 100644 --- a/apps/ui/src/app/dashboard/page.tsx +++ b/apps/ui/src/app/dashboard/page.tsx @@ -6,18 +6,20 @@ import { fetchServerData } from "@/lib/server-api"; import type { User } from "@/lib/types"; export default async function DashboardPage() { - // Fetch user data server-side - const initialUserData = await fetchServerData< + // Fetch user and organization data server-side in parallel + const initialUserDataPromise = fetchServerData< { user: User } | undefined | null >("GET", "/user/me"); + const initialOrganizationsDataPromise = fetchServerData("GET", "/orgs"); + + const initialUserData = await initialUserDataPromise; // Redirect to login if not authenticated if (!initialUserData?.user) { redirect("/login"); } - // Fetch organizations server-side - const initialOrganizationsData = await fetchServerData("GET", "/orgs"); + const initialOrganizationsData = await initialOrganizationsDataPromise; // Check if organizations data is null (API error) if (!initialOrganizationsData) {