#31
Make large mailboxes blazingly fast
Summary
- Add provider-native cursor pagination using Gmail page tokens and JMAP anchors, while retaining offset compatibility for existing routes.
- Automatically load continuation pages and virtualize mailboxes above 100 conversations so only the visible window and overscan rows stay mounted.
- Restore accumulated folder pages from a bounded, account-scoped cache while merging fresh first-page state after navigation.
- Keep existing mail visible when continuation loading fails, preserve keyboard focus across virtual windows, and expose accurate list semantics to assistive technology.
- Add mailbox list
Server-Timinginstrumentation and an opt-in 10,000-thread demo fixture for repeatable performance testing.
Verification
pnpm exec eslint <changed TypeScript and JavaScript files>pnpm exec tsc --noEmitpnpm test(147 tests passed after rebasing onto the latestmain)pnpm buildgit diff --check origin/main...HEAD
Browser evidence
- Tested a synthetic 10,000-thread mailbox with 8,122 conversations in Inbox.
- After loading 120 conversations, the virtual list kept 28 rows mounted with zero recorded long tasks.
- Pagination issued exactly one request for each cursor (
20,40,60,80,100). - Opening an unread conversation and returning restored the accumulated list without another list request, while retaining the fresh read state.
- Verified keyboard focus remains mounted across a virtual-window scroll.
- Verified a 320px viewport has no horizontal overflow and a hard refresh returns without runtime or console errors.
- Rechecked the newly merged Focus route inside the combined app shell and returned to the accumulated virtualized Inbox without runtime errors.
- A local demo continuation response measured 12.8ms total, including 7.9ms reported by
mail-listserver timing, at roughly 8.1KB transferred.
Data boundary
This change does not persist Gmail or JMAP message bodies in browser storage. It removes the list and DOM scaling bottlenecks without expanding mailbox data retention.
Activity
8Addressed the stale folder pagination race in e483e31.
FolderResultsis now keyed by account sync scope, folder, and list query, so folder navigation cannot reuse the previous folder's stateful results instance.- Continuation requests now use an
AbortController; changing the initial page or unmounting the results aborts the old request. - A response, error, or
finallyblock may update list/loading state only while its controller is still the active request, so stale work cannot overwrite the new folder or clear a newer load.
Verification:
- Reproduced the reported sequence with a 10,000-thread demo mailbox and an injected 10-second delay on the Inbox continuation request.
- Navigated to Sent while the Inbox request was pending:
delayed: 1,aborted: 1,completed: 0. - After waiting past the original completion time, the page remained on Sent with only Sent thread IDs, no load-error state, and no console/runtime errors.
- Focused ESLint and TypeScript checks passed.
- Full test suite passed: 147/147.
- Production build and
git diff --check origin/main...HEADpassed.
fa25ef3 into main2mo ago
Review
Note: my token only has
pulls:commentscope, notpulls:write, so I can't submit a formal review decision (approve/request-changes) or anchor inline comments — posting findings as a regular comment instead.Blocking: stale-closure overwrite in
loadMore()can leak the wrong folder's mail into viewcomponents/shell/app-shell.tsx:1494(insideFolderResults.loadMore):loadMore()closes overthreads,folder, andpageQueryStringfrom the render that invoked it, and thissetThreads(merged)is an unconditional overwrite rather than a functional state update.FolderResultsis rendered withkey={threadQueryToSearch(query).toString()}(app-shell.tsx:1271) — that key does not includefolder. So navigating from one folder to another with the same filters (the common case: no search/sort applied) does not remountFolderResults; it just updates props, and thesourcePage !== initialPagerender-time check (app-shell.tsx:1365-1369) resetsthreads/hasMore/nextCursorto the new folder's data in place, on the same component instance.Concrete repro:
onMorefiresloadMore(). The fetch (folder=inbox) is in flight.threadsto Sent's initial page via the render-time reset.mergeThreadPages(threads, page.threads)using the closure-captured Inboxthreadsarray (not current React state), thensetThreads(merged)overwrites the just-reset Sent list with a mix of stale Inbox threads + newly-fetched Inbox threads — while the visible header/chrome still says "Sent".rememberFolderPage(folder, pageQueryString, …)also uses the stale closure, so it happens to write to the correct (inbox) cache key, but the live UI state shown to the user is now wrong-folder mail.The prior implementation (
setThreads((current) => [...current, ...page.threads])) used a functional updater, which at least applied on top of whatever the latest state was rather than reintroducing an entirely stale, wrong-folder array — this is a regression introduced by this PR's cursor/merge rework, not pre-existing behavior.Suggested fix: guard the completion against staleness (e.g. capture
initialPage/folderin a ref at call time and bail out in the.then/finallyif they've since changed), and/or switch back to a functionalsetThreadsupdate so a stale completion can't clobber unrelated state.Everything else checked out
lib/gmail/provider-core.ts,lib/gmail/provider.ts,lib/stalwart/client.ts,lib/stalwart/provider.ts): offset/cursor mode selection,nextPageToken/anchor propagation, and the explicitanchor: undefinedreset on the parallel unread-count JMAP call all line up with their new tests.lib/mail/virtual-list.ts: range math, row-height table,mergeThreadPagesdedup-by-id, andrestoreCachedThreadPage's equality guard (falls back to the fresh page whenevertotalor the leading thread IDs don't match) look correct and are covered by tests exercising the edge cases (clamped windows, changed leading page → no restore).normalizeCursorbounds cursor length to 2048 chars at the sharedquery-params.tsboundary, which both the client and the/api/mail/threadsroute go through — no unbounded input reaches the provider calls.${account.id}:${account.syncRevision}and bounded to 12 entries (MAX_FOLDER_PAGE_CACHE_ENTRIES), so it can't grow unbounded or leak stale data across a sync that changes folder contents.role="list"/role="listitem",aria-posinset/aria-setsize(with aMath.maxguard against an under-counted providertotalestimate), and focus tracking viadata-thread-idlook consistent with the PR's stated goals.Verification performed
Read the full diff against
origin/mainin the checked-out PR branch and cross-referenced changed call sites directly (not just the diff), traced the render-time state-reset pattern and theFolderResultskey to confirm the race above is reachable via ordinary folder-to-folder navigation, and spot-checked the new unit tests (virtual-list.test.mjs,provider-core.test.mjs,client.test.mjs,query-params.test.mjs) against the implementations they cover.