#27

Speed up mail loading and tab navigation

MergedstylesshOwnermerged intofrom

Summary

  • reuse one mail provider and mailbox client across each server render
  • batch Stalwart JMAP queries, message hydration, and thread hydration while deduplicating session and mailbox discovery requests
  • replace eight serial hosted-mail sidebar count queries with mailbox metadata and a single starred query
  • parallelize Gmail unread and folder-count work
  • retain recently opened dynamic routes for 30 seconds so workspace tab revisits do not issue another route request

Performance evidence

Authenticated production baseline before this change:

  • fresh Inbox: 204 ms TTFB, 2.69 s response completion, 2.74 s LCP
  • Inbox tab transition: 1.05 s
  • conversation tab transition: 1.35 s

Local optimized production build:

  • first conversation navigation: 42 ms
  • Inbox tab revisit: 35 ms with no route request
  • conversation tab revisit: 23 ms with no route request

Live after-deployment measurements are still required because the provider changes are not yet deployed.

Validation

  • corepack pnpm test - 117 passed
  • corepack pnpm exec tsc --noEmit - passed
  • ESLint across changed TypeScript files - passed
  • corepack pnpm build - passed with Next.js 16.3.3
  • git diff --check origin/main...HEAD - passed

Existing limitation

  • repository-wide pnpm lint still reports the two pre-existing CommonJS require findings in babel.config.js and postcss.config.js; neither file is changed here

Activity

4
mikey3mo ago

Review

Went through the diff (gmail/provider.ts, mail/server.ts, stalwart/client.ts + tests, stalwart/folder-counts.ts + tests, stalwart/provider.ts, next.config.ts), plus the unchanged call sites in components/shell/app-shell.tsx and components/mail/thread-list.tsx that consume the mail providers, since the perf changes touch shared caching/dedup paths those components depend on.

Blocking

next.config.ts (lines 7-11): staleTimes.dynamic: 30 combined with existing router.replace() usage in app-shell.tsx can show a stale folder list after archive/move/mark-unread.

Setting staleTimes.dynamic: 30 means a router.replace()/router.push() back to an already-visited folder route reuses the cached RSC payload for up to 30s instead of refetching. Several mutation flows in components/shell/app-shell.tsx rely on a plain returnToFolder() (router.replace(mailFolderHref(...)), line ~1500) to reflect a change, with no accompanying router.refresh():

  • onArchive (~lines 1576-1580): archives the open thread, then calls returnToFolder().
  • onMove (~lines 1581-1590): same pattern for move-to-inbox/spam/trash.
  • onMarkUnread (~lines 1564-1569): same pattern for marking a thread unread from the toolbar.

Concretely: open a thread from Inbox, click Archive, get routed back to Inbox — the archived thread can still show in the list, because it was never locally removed from that route's data (unlike the optimistic removal thread-list.tsx does for actions taken directly on a list row), while the sidebar unread/archived counts have already changed via adjustArchiveCounts. That leaves the visible list and the sidebar counts out of sync until the 30s window lapses or another navigation forces a refetch.

Note onStar (line 1574) and onCollection (line 1613) in the same file already call router.refresh() after their mutation for exactly this reason — onArchive/onMove/onMarkUnread need the same treatment now that dynamic routes are cached for 30s.

Looks solid

  • StalwartClient.connect()/listMailboxes() request de-duplication (connectPromise, mailboxRequests map) is scoped per client instance, and client instances are created fresh per request (via mailboxClient/loadMailProvider's cache()), so there's no cross-request staleness from the new in-instance caching.
  • listEmails/getThreadEmails batching in lib/stalwart/client.ts preserves the existing fallback behavior for legacy thread/email id URLs, and responseFor correctly surfaces JMAP-level errors instead of silently returning undefined.
  • lib/gmail/provider.ts#getFolderCounts refactor to run label hydration and the archived-count query in parallel preserves the original merge semantics (Object.fromEntries(entries) still overrides the defaults the same way as before).
  • lib/mail/server.ts's new loadMailProvider cache correctly reuses requireSessionUser() (itself cache()-wrapped) instead of introducing a second, uncoordinated cache.
  • New tests in client.test.mjs and folder-counts.test.mjs reasonably cover the new dedup/batching/mailbox-metadata logic.

Non-blocking

  • lib/stalwart/provider.ts#listThreads's new starred folder path (bypassing the mailboxId early-return and filtering by $flagged keyword instead of inMailbox) has no test coverage — worth a case in a provider-level test if one gets added, since it's a new query shape rather than a refactor of existing behavior.
  • The new getMailboxClient() memoization in stalwart/provider.ts is only used by the read paths (listThreads, getThread, getFolderCounts, listCollections); the write paths (createCollection, archiveThread, moveThread, send, etc.) still call mailboxClient(user) directly and re-authenticate a fresh client each time. Not a bug, but the "reuse one mailbox client" win from the PR description doesn't apply to mutation-heavy flows.

Note: I only have pulls:comment scope on this run, not pulls:write, so I couldn't submit this as a formal review with inline line comments — posting as a single PR comment instead.

stylessh merged commit c336ffc into main3mo ago

Branch stylessh/faster-mail-navigation has been merged.

Checking branch