From 046ff76395e8e1e1a7cbce2fe9fd9220ef67788f Mon Sep 17 00:00:00 2001 From: Richie McIlroy Date: Fri, 13 Nov 2026 11:44:40 -0500 Subject: [PATCH 1/3] perf(web): load sign-in and email code only when a request needs it The developer API and hosted MCP routes imported next-auth, SSO and the React Email templates at module load through three static paths: the Hono auth helpers, the web-backend Auth service, and the Infra billing emails. Every cold start evaluated them although only cookie-authenticated requests and billing emails use them. Import them where they are used instead. --- apps/web/app/api/utils.ts | 3 ++- apps/web/lib/infra-billing-emails.ts | 11 ++++++++--- packages/web-backend/src/Auth.ts | 8 ++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/web/app/api/utils.ts b/apps/web/app/api/utils.ts index 0e97ce97aea..934023d0216 100644 --- a/apps/web/app/api/utils.ts +++ b/apps/web/app/api/utils.ts @@ -1,7 +1,7 @@ import { db } from "@cap/database"; import { isBlockedAccountEmail } from "@cap/database/auth/domain-utils"; import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site"; -import { getCurrentUser } from "@cap/database/auth/session"; +import type { getCurrentUser } from "@cap/database/auth/session"; import { authApiKeys, developerApiKeys, @@ -493,6 +493,7 @@ async function getAuth(c: Context) { user = res[0]?.users; if (user && isBlockedAccountEmail(user.email)) user = undefined; } else if (!isCrossSiteCookieWrite(c.req.raw)) { + const { getCurrentUser } = await import("@cap/database/auth/session"); user = await getCurrentUser(); } diff --git a/apps/web/lib/infra-billing-emails.ts b/apps/web/lib/infra-billing-emails.ts index 984832265f5..eb0d3775c3a 100644 --- a/apps/web/lib/infra-billing-emails.ts +++ b/apps/web/lib/infra-billing-emails.ts @@ -1,7 +1,4 @@ import { db } from "@cap/database"; -import { sendEmail } from "@cap/database/emails/config"; -import { InfraOutOfCredit } from "@cap/database/emails/infra-out-of-credit"; -import { InfraTopUpFailed } from "@cap/database/emails/infra-top-up-failed"; import { developerApps, developerCreditAccounts, @@ -58,6 +55,10 @@ export async function sendInfraOutOfCreditEmail({ try { const recipient = await recipientFor(accountId); if (!recipient) return false; + const [{ sendEmail }, { InfraOutOfCredit }] = await Promise.all([ + import("@cap/database/emails/config"), + import("@cap/database/emails/infra-out-of-credit"), + ]); const result = await sendEmail({ email: recipient.email, subject: `${recipient.appName} is out of Cap Infra credit`, @@ -87,6 +88,10 @@ export async function sendInfraTopUpFailedEmail({ try { const recipient = await recipientFor(accountId); if (!recipient) return false; + const [{ sendEmail }, { InfraTopUpFailed }] = await Promise.all([ + import("@cap/database/emails/config"), + import("@cap/database/emails/infra-top-up-failed"), + ]); await sendEmail({ email: recipient.email, subject: `The auto top-up for ${recipient.appName} was declined`, diff --git a/packages/web-backend/src/Auth.ts b/packages/web-backend/src/Auth.ts index cab0346dc83..2c35972aafe 100644 --- a/packages/web-backend/src/Auth.ts +++ b/packages/web-backend/src/Auth.ts @@ -1,4 +1,3 @@ -import { getServerSession } from "@cap/database/auth/auth-options"; import { isBlockedAccountEmail } from "@cap/database/auth/domain-utils"; import { isCrossSiteCookieWrite } from "@cap/database/auth/request-site"; import * as Db from "@cap/database/schema"; @@ -31,7 +30,12 @@ export const getCurrentUser = Effect.gen(function* () { return Option.none(); return yield* Option.fromNullable( - yield* Effect.tryPromise(() => getServerSession()), + yield* Effect.tryPromise(async () => { + const { getServerSession } = await import( + "@cap/database/auth/auth-options" + ); + return getServerSession(); + }), ).pipe( Option.map((session) => Effect.gen(function* () { From d20b20c3fd873c2618ec1a010db6cc7b01047fed Mon Sep 17 00:00:00 2001 From: Richie McIlroy Date: Fri, 13 Nov 2026 11:44:40 -0500 Subject: [PATCH 2/3] test(web): keep sign-in and email code out of the developer API startup graph --- .../unit/developer-api-cold-path.test.ts | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 apps/web/__tests__/unit/developer-api-cold-path.test.ts diff --git a/apps/web/__tests__/unit/developer-api-cold-path.test.ts b/apps/web/__tests__/unit/developer-api-cold-path.test.ts new file mode 100644 index 00000000000..96e717c8b13 --- /dev/null +++ b/apps/web/__tests__/unit/developer-api-cold-path.test.ts @@ -0,0 +1,85 @@ +import { fileURLToPath } from "node:url"; +import { build, type Metafile } from "esbuild"; +import { describe, expect, it } from "vitest"; + +// A cold start of a developer API function evaluates every module the route +// imports statically. Sign-in (next-auth, SSO) and email rendering are only +// needed by cookie-authenticated requests and by billing emails, so they must +// stay behind dynamic imports and out of these routes' startup graph. +const webRoot = fileURLToPath(new URL("../../", import.meta.url)); + +const ROUTES = [ + "app/api/developer/v1/[...route]/route.ts", + "app/api/developer/mcp/route.ts", +]; + +const NOT_AT_STARTUP = [ + "packages/database/auth/auth-options", + "packages/database/auth/session", + "packages/database/emails/", + "node_modules/next-auth/", + "node_modules/nodemailer/", + "node_modules/@workos-inc/", + "node_modules/@react-email/", + "node_modules/resend/", +]; + +async function staticGraph(route: string) { + const result = await build({ + entryPoints: [route], + absWorkingDir: webRoot, + bundle: true, + write: false, + metafile: true, + platform: "node", + format: "esm", + logLevel: "silent", + tsconfig: "tsconfig.json", + conditions: ["react-server", "node"], + external: ["next", "next/*", "@vercel/*"], + loader: { + ".node": "empty", + ".wasm": "empty", + ".svg": "empty", + ".png": "empty", + ".css": "empty", + }, + }); + const inputs: Metafile["inputs"] = result.metafile.inputs; + const entry = Object.keys(inputs).find((path) => path.endsWith(route)); + if (!entry) throw new Error(`entry not found for ${route}`); + const parent = new Map([[entry, null]]); + const queue = [entry]; + while (queue.length > 0) { + const current = queue.shift() as string; + for (const imported of inputs[current]?.imports ?? []) { + if (imported.kind === "dynamic-import") continue; + if (parent.has(imported.path) || !inputs[imported.path]) continue; + parent.set(imported.path, current); + queue.push(imported.path); + } + } + return parent; +} + +function chainTo(parent: Map, path: string) { + const chain: string[] = []; + for (let at: string | null | undefined = path; at; at = parent.get(at)) { + chain.unshift(at.replace(/^.*node_modules\//, "")); + } + return chain.join(" -> "); +} + +describe("developer API cold path", () => { + it.each(ROUTES)( + "%s does not load sign-in or email code at startup", + async (route) => { + const parent = await staticGraph(route); + const offenders = [...parent.keys()] + .filter((path) => NOT_AT_STARTUP.some((part) => path.includes(part))) + .map((path) => chainTo(parent, path)); + expect(offenders.slice(0, 3)).toEqual([]); + }, + 60_000, + ); +}); From 4001d571fc078b037691bd0cb72b5ea9eb54397c Mon Sep 17 00:00:00 2001 From: Richie McIlroy Date: Fri, 13 Nov 2026 11:51:23 -0500 Subject: [PATCH 3/3] chore(web): declare esbuild for the developer API cold path test --- apps/web/package.json | 1 + bun.lock | 1 + 2 files changed, 2 insertions(+) diff --git a/apps/web/package.json b/apps/web/package.json index a350f50db82..62536495c12 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -174,6 +174,7 @@ "@vitest/ui": "^3.2.0", "autoprefixer": "^10.4.14", "cross-env": "^7.0.3", + "esbuild": "0.25.12", "eslint": "^9.30.1", "eslint-config-next": "16.4.5", "glob": "^14.0.4", diff --git a/bun.lock b/bun.lock index 45a0f913148..18e0d8f9710 100644 --- a/bun.lock +++ b/bun.lock @@ -273,6 +273,7 @@ "@vitest/ui": "^3.2.0", "autoprefixer": "^10.4.14", "cross-env": "^7.0.3", + "esbuild": "0.25.12", "eslint": "^9.30.1", "eslint-config-next": "16.4.5", "glob": "^14.0.4",