Skip to content

perf(web): load sign-in and email code only when a developer API request needs it - #4229

Merged
richiemcilroy merged 5 commits into
mainfrom
perf/infra-api-cold-path
Nov 13, 2026
Merged

richiemcilroy merged 5 commits into
mainfrom
perf/infra-api-cold-path

Conversation

@richiemcilroy

@richiemcilroy richiemcilroy commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

What

The developer API (/api/developer/v1/[...route]) and the hosted MCP route (/api/developer/mcp) loaded sign-in and email code at module load. That code is next-auth, SSO (WorkOS), nodemailer, Resend and the React Email templates. Three static imports pulled it in:

  • apps/web/app/api/utils.ts imported getCurrentUser from @cap/database/auth/session for the cookie branch of getAuth.
  • packages/web-backend/src/Auth.ts imported getServerSession from @cap/database/auth/auth-options for the session branch of getCurrentUser.
  • apps/web/lib/infra-billing-emails.ts imported sendEmail and the two Infra email templates at the top.

Each now imports the module where it is used, so it loads on the first request or email that needs it. Behaviour is unchanged: the same functions run with the same arguments in the same order. Cookie-authenticated routes load the session module on their first request instead of at startup.

Why

A cold start evaluates every module in the route's static import graph. Secret-key and upload-token requests never use the session or email code. Measured on a local bundle of the route, built with code splitting as Next.js does:

Loaded at startup Module load, median of 6
main 11.4 MB about 258 ms
this PR 8.3 MB about 193 ms

On the preview deployment of this branch:

  • The developer API answers as on production. /v1/usage with no key returns 401, an unknown secret key returns 401 invalid_secret_key, and the MCP route returns 401.
  • The two moved session paths still run and resolve their dynamic import on a real deployment. GET /api/developer/credits/transactions (Hono withAuth, cookie branch) and DELETE /api/video/delete (HttpAuthMiddleware, session branch) return the same 401 status and body on the preview as on production, not a 500.

Tests

  • New __tests__/unit/developer-api-cold-path.test.ts bundles both routes with esbuild and walks the static import graph. It fails on main with the import chain (route.ts -> app.ts -> api/utils.ts -> auth/session.ts -> next-auth) and passes here.
  • The existing tests for these paths pass unchanged, including the cookie-write and auth middleware tests and the Infra out-of-credit and auto top-up email tests. Those tests mock the email and session modules, and the mocks still apply to the dynamic imports.
  • The whole apps/web unit suite passes, apart from the 7 share-stage-identity failures that are the same on main under Node 26. The repo typecheck (bun run typecheck) and Biome pass.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no new actionable issues were found.

Summary

This PR loads sign-in and billing email code only when a request needs it.

  • Developer API routes wait for a cookie session before loading sign-in code.
  • Web-backend auth waits for a session lookup before loading sign-in code.
  • Infra billing emails load their sender and template only when sending.

No new actionable issues were found. Tests were not run during this review.

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

Richie McIlroy added 2 commits November 13, 2026 11:44
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.
@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@sandchest-com

sandchest-com Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Sandchest

Sandchest reviewed 510eaa4: nothing exploitable found in this change.

Sandchest reviews every change for vulnerabilities an attacker could use · Review details

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

Open in Capy

@vercel

vercel Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
cap-status Skipped Skipped Nov 13, 2026 5:59pm UTC

@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 13, 2026 17:50 Inactive
@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 13, 2026 17:59 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

Open in Capy

@richiemcilroy
richiemcilroy merged commit 49ab0f6 into main Nov 13, 2026
42 checks passed

This branch was successfully deployed

1 active and 1 inactive deployments
Preview – cap-web — 510eaa47 Deployed Nov 13, 2026 by vercel[bot]
Preview – cap-status — 510eaa47 Deployed Nov 13, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant