#31

Make large mailboxes blazingly fast

MergedstylesshOwnermerged intofrom

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-Timing instrumentation 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 --noEmit
  • pnpm test (147 tests passed after rebasing onto the latest main)
  • pnpm build
  • git 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-list server 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

8
stylessh force-pushed the stylessh/blazing-mail-client branch from f78ac3e to 6e6f4c42mo ago
mikey2mo ago

Review

Note: my token only has pulls:comment scope, not pulls: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 view

components/shell/app-shell.tsx:1494 (inside FolderResults.loadMore):

const merged = mergeThreadPages(threads, page.threads);
setThreads(merged);

loadMore() closes over threads, folder, and pageQueryString from the render that invoked it, and this setThreads(merged) is an unconditional overwrite rather than a functional state update.

FolderResults is rendered with key={threadQueryToSearch(query).toString()} (app-shell.tsx:1271) — that key does not include folder. So navigating from one folder to another with the same filters (the common case: no search/sort applied) does not remount FolderResults; it just updates props, and the sourcePage !== initialPage render-time check (app-shell.tsx:1365-1369) resets threads/hasMore/nextCursor to the new folder's data in place, on the same component instance.

Concrete repro:

  1. User scrolls to the bottom of Inbox; onMore fires loadMore(). The fetch (folder=inbox) is in flight.
  2. Before it resolves, the user clicks into Sent. The prop update resets threads to Sent's initial page via the render-time reset.
  3. The stale Inbox fetch resolves. Its continuation computes mergeThreadPages(threads, page.threads) using the closure-captured Inbox threads array (not current React state), then setThreads(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/folder in a ref at call time and bail out in the .then/finally if they've since changed), and/or switch back to a functional setThreads update so a stale completion can't clobber unrelated state.

Everything else checked out

  • Gmail/JMAP cursor plumbing (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 explicit anchor: undefined reset on the parallel unread-count JMAP call all line up with their new tests.
  • lib/mail/virtual-list.ts: range math, row-height table, mergeThreadPages dedup-by-id, and restoreCachedThreadPage's equality guard (falls back to the fresh page whenever total or 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).
  • Cursor validation: normalizeCursor bounds cursor length to 2048 chars at the shared query-params.ts boundary, which both the client and the /api/mail/threads route go through — no unbounded input reaches the provider calls.
  • Folder page cache: now scoped by ${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.
  • Accessibility: role="list"/role="listitem", aria-posinset/aria-setsize (with a Math.max guard against an under-counted provider total estimate), and focus tracking via data-thread-id look consistent with the PR's stated goals.

Verification performed

Read the full diff against origin/main in the checked-out PR branch and cross-referenced changed call sites directly (not just the diff), traced the render-time state-reset pattern and the FolderResults key 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.

stylesshOwner2mo ago

Addressed the stale folder pagination race in e483e31.

  • FolderResults is 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 finally block 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...HEAD passed.
stylessh merged commit fa25ef3 into main2mo ago

Branch stylessh/blazing-mail-client has been merged.

Checking branch