#27
Speed up mail loading and tab navigation
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 passedcorepack pnpm exec tsc --noEmit- passed- ESLint across changed TypeScript files - passed
corepack pnpm build- passed with Next.js 16.3.3git diff --check origin/main...HEAD- passed
Existing limitation
- repository-wide
pnpm lintstill reports the two pre-existing CommonJSrequirefindings inbabel.config.jsandpostcss.config.js; neither file is changed here
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 incomponents/shell/app-shell.tsxandcomponents/mail/thread-list.tsxthat 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: 30combined with existingrouter.replace()usage inapp-shell.tsxcan show a stale folder list after archive/move/mark-unread.Setting
staleTimes.dynamic: 30means arouter.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 incomponents/shell/app-shell.tsxrely on a plainreturnToFolder()(router.replace(mailFolderHref(...)), line ~1500) to reflect a change, with no accompanyingrouter.refresh():onArchive(~lines 1576-1580): archives the open thread, then callsreturnToFolder().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.tsxdoes for actions taken directly on a list row), while the sidebar unread/archived counts have already changed viaadjustArchiveCounts. 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) andonCollection(line 1613) in the same file already callrouter.refresh()after their mutation for exactly this reason —onArchive/onMove/onMarkUnreadneed the same treatment now that dynamic routes are cached for 30s.Looks solid
StalwartClient.connect()/listMailboxes()request de-duplication (connectPromise,mailboxRequestsmap) is scoped per client instance, and client instances are created fresh per request (viamailboxClient/loadMailProvider'scache()), so there's no cross-request staleness from the new in-instance caching.listEmails/getThreadEmailsbatching inlib/stalwart/client.tspreserves the existing fallback behavior for legacy thread/email id URLs, andresponseForcorrectly surfaces JMAP-level errors instead of silently returningundefined.lib/gmail/provider.ts#getFolderCountsrefactor 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 newloadMailProvidercache correctly reusesrequireSessionUser()(itselfcache()-wrapped) instead of introducing a second, uncoordinated cache.client.test.mjsandfolder-counts.test.mjsreasonably cover the new dedup/batching/mailbox-metadata logic.Non-blocking
lib/stalwart/provider.ts#listThreads's newstarredfolder path (bypassing themailboxIdearly-return and filtering by$flaggedkeyword instead ofinMailbox) 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.getMailboxClient()memoization instalwart/provider.tsis only used by the read paths (listThreads,getThread,getFolderCounts,listCollections); the write paths (createCollection,archiveThread,moveThread,send, etc.) still callmailboxClient(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:commentscope on this run, notpulls:write, so I couldn't submit this as a formal review with inline line comments — posting as a single PR comment instead.