#28

Make conversation navigation instant after prefetch

MergedstylesshOwnermerged intofrom

Summary

  • set the conversation title immediately from the clicked row instead of fetching the conversation once for metadata and again for the page
  • remove the folder-list fetch from normal conversation navigation and reuse the list already rendered in the persistent mail shell
  • cache prefetched conversation detail for 30 seconds in private browser memory, with account-scoped invalidation after mailbox writes
  • start custom-folder validation and conversation detail reads in parallel; direct deep links load previous/next navigation lazily after the conversation renders

Why

The row already has the subject, so it should own the immediate title update. It does not contain full message bodies or attachments, so the conversation detail still needs one provider read.

The previous route looked parallel at the page level, but Next.js resolved dynamic metadata first. That serialized one conversation read ahead of the page's conversation + folder reads. Even after removing that lifecycle waterfall, a normal click still fetched a folder page the persistent shell already had and hover prefetch was not reusable because personalized mail was uncached.

Performance evidence

  • current production first-time conversation navigation: about 1.16s
  • current production provider reads: 1.09-1.17s sequentially versus 564-587ms in parallel
  • local optimized production trace after this change: one RSC request on row hover/focus and zero /mail or /api/mail requests on click
  • direct hard load renders the conversation first, then fetches its navigation list lazily
  • cached tab return remains about 16ms under the existing 30-second route stale time

Cache correctness

  • conversation detail uses use cache: private, so mail content is kept only in the current browser's memory and is never stored in a shared server cache
  • cache entries are tagged by account and invalidated after read, star, archive, move, and collection mutations
  • mutations that leave a conversation perform a fresh folder navigation so stale route state cannot reintroduce removed mail

Verification

  • pnpm test (117 passed)
  • pnpm exec tsc --noEmit
  • focused ESLint on all changed files
  • pnpm build
  • production-mode browser: title updates immediately and survives hard refresh
  • production-mode browser: hover/focus prefetch is reused on click; previous/next uses the existing folder page
  • production-mode browser: direct deep link restores previous/next via lazy list loading
  • production-mode browser: archive returns to a fresh Inbox with the archived thread absent
  • full pnpm lint still reports only the two pre-existing no-require-imports errors in babel.config.js and postcss.config.js

Activity

7
mikeyreviewed3mo ago
mikey3mo ago

Review summary

Small, well-scoped diff (2 files, +7/-7). Verified against surrounding code, not just the patch.

app/.../thread/[threadId]/page.tsx — replacing the per-thread generateMetadata (which called loadThreadDetail) with a static metadata object is safe: loadThreadDetail is wrapped in React's cache() (lib/mail/server.ts:39), so it was already memoized per-request — the real cost was Next.js blocking page rendering until generateMetadata resolved, serializing the thread fetch ahead of the folder fetch. Removing it restores true parallelism via the existing Promise.all in AccountThreadContent. Matches the PR's measured 1.09-1.17s → 564-587ms evidence.

components/shell/app-shell.tsx

  • New document.title effect in ThreadRoute only — I checked whether DraftRoute (the other consumer of this same page/metadata) loses dynamic titling. It doesn't matter: DraftRoute immediately router.replaces away to the drafts folder in a useEffect and only ever renders a loading placeholder, so the generic "Conversation" title is never user-visible there.
  • returnToFolder now calls router.replace(...) followed by router.refresh(). This is not a novel pattern — the same file already uses router.push/replace immediately followed by router.refresh() elsewhere (lines ~691-692 send-delivery handling, ~712-713 sign-out), so the ordering is known-safe in this codebase.
  • The added early return after returnToFolder() in the onCollection handler just avoids a redundant second router.refresh() call now that returnToFolder performs its own refresh — not a behavior change.

No correctness, security, or data-integrity issues found. No test changes accompany this PR, but none of the pre-existing test suite touched generateMetadata or these router callbacks either, so there's no coverage regression, just no new coverage for the title effect / refresh-on-mutation behavior (non-blocking).

Verification performed: read the full diff, loadThreadDetail/loadFolderPage cache definitions, ThreadRoute/DraftRoute in full, the legacy /mail/[folder]/thread/[threadId] redirect-only page (unaffected), and existing router.replace/push + router.refresh() call sites in the same file for precedent.

stylessh changed the title from Remove thread navigation waterfall to Make conversation navigation instant after prefetch3mo ago
stylessh merged commit 44ab134 into main3mo ago

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

Checking branch