Skip to content

perf(chat): load the chat timeline and LegendList as their own chunk - #977

Merged
devswha merged 3 commits into
mainfrom
perf/lazy-chat-timeline
Oct 13, 2026
Merged

devswha merged 3 commits into
mainfrom
perf/lazy-chat-timeline

Conversation

@devswha

@devswha devswha commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Problem

After #951 the entry chunk grew from about 475 KB to about 572 KB gzip. Part of that is @legendapp/list, which ChatTimeline pulls in, and ChatTimeline was a static import of ChatView. So every page load parsed the list library, even for someone who never opens the chat lens.

Root cause

ChatView.tsx did import { ChatTimeline } from "./ChatTimeline.tsx", and ChatView is a static import of PaneTerminal. That put the timeline and @legendapp/list in the entry chunk.

The list is only about 42 KB of the roughly 97 KB #951 added. The rest is React 19 and the other #951 code. This PR does not touch that.

Change

  • New src/components/chatTimelineLoader.ts. It runs import("./ChatTimeline.tsx") once, when the entry runs, so the chunk is fetched right after the entry and not when chat is opened. It keeps the loaded component and exposes useChatTimeline() (a useSyncExternalStore). If the load fails, the loader forgets it so the next read tries again. It does not use React.lazy, because that can suspend once even when the module has already loaded.
  • ChatView's conversation read waits for the chunk (await loadChatTimeline()) before it sets any turn. Once the chunk has loaded, this check costs nothing. Because of the wait, the list never mounts with a partly loaded conversation: it mounts once, with scroller and timeline bound, and the restore, initial scroll-to-end and onRead effects see the list.
  • Until the chunk arrives, ChatView shows the same loading state (header, then the "Loading conversation…" footer) inside the same .chat-view.chat-view-virtual / .chat-transcript-virtual boxes, just without the list. The two effects that bind the scroll node once (ResizeObserver follow and dismissKeyboardOn) now depend on the loaded component too, so they bind to the list's node when it mounts.
  • ChatTimeline.css is still imported from ChatView, so it stays in the entry CSS. .chat-frame, .chat-frame-body and the loading state need it before the chunk arrives, so no styles change late.
  • src/AGENTS.md (Chat lens row) describes the split.

Trade-off: the entry gets smaller, so there is less to parse and compile before first paint. The total bytes of a cold load stay the same, because the chunk is fetched right away and not on demand. If the chunk cannot be fetched (offline after an update, for example), the chat error line shows the browser's "Failed to fetch dynamically imported module" text, and the next poll tries again. The other lazy viewers (RepoDiff, VirtualSource) already behave this way. The service worker caches every hashed /assets/ file, so the new chunk is cached like the others.

Measurements

bun run build. Before is origin/main 9dce921, after is this branch.

before after change
entry index-*.js raw 1,876.45 kB 1,744.59 kB -131.9 kB
entry gzip (vite report) 586.91 kB 544.74 kB -42.2 kB
entry .js.gz served 579,664 B 537,846 B -41.8 KB
entry .js.br served 498,834 B 459,318 B -39.5 KB
new ChatTimeline-*.js none 132.48 kB raw / 42.58 kB gzip

LegendList no longer appears in the entry chunk. No CSS chunk was split off.

Validation

Linux x64, Bun 1.4.3.

  • bun run check fast: all steps ok (typecheck, build, unit tests 3455 pass / 0 fail).
  • These three were run one after another on the pushed commit (28591de), with no source changes during the runs, and each exited 0: CI=1 bun run check run --build bun scripts/ui-regression.ts, ... scripts/chat-fallback-browser-qa.ts and ... scripts/chat-virtual-browser-qa.ts.
  • A one-off browser run, not committed. The chat-virtual fixture was built with code splitting, and the request for the chunk holding LegendList was held. While it was held: the chunk was a separate request, the conversation read had happened, "Loading conversation…" showed, there were no turn rows, and .chat-frame was position: absolute. After the chunk was released: rows rendered, the view sat at the end (row 199 visible), and after the viewport was narrowed the view was still at the end, so the ResizeObserver was bound to the list's node.

🤖 Generated with Claude Code

The chat timeline (and @legendapp/list under it) was a static import of
ChatView, so it sat in the entry chunk. It now loads through
components/chatTimelineLoader.ts: the import starts when the entry runs,
and a conversation read waits for it before setting any turn, so the list
mounts once with ChatView's refs bound. Until it arrives ChatView draws
the same loading state without the list. ChatTimeline.css stays in the
entry because .chat-frame and the loading state need it first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 19 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a2b69e19-08f3-4446-9f87-04aa511ae40c
📥 Commits

Reviewing files that changed from the base of the PR and between 6a9a2f9 and 0bf25f6.

📒 Files selected for processing: 5
  • changelog.d/lazy-chat-timeline.md
  • src/AGENTS.md
  • src/components/ChatTimeline.tsx
  • src/components/ChatView.tsx
  • src/components/chatTimelineLoader.ts

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f4e2199d-7e64-4f57-ab78-c9c31c14d904
📥 Commits

Reviewing files that changed from the base of the PR and between 2e99de9 and 6a9a2f9.

📒 Files selected for processing: 5
  • changelog.d/lazy-chat-timeline.md
  • src/AGENTS.md
  • src/components/ChatTimeline.tsx
  • src/components/ChatView.tsx
  • src/components/chatTimelineLoader.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Performance
    • Reduced the app’s initial script size by about 42 KB after gzip by loading the chat timeline separately.
  • Bug Fixes
    • Ensured conversation turns appear after the chat timeline is ready, while keeping the conversation header and footer visible during loading.

The chat timeline and its list library load in a separate chunk. ChatView waits for the timeline before processing non-scrollback conversation reads and renders turns when the component is available. The timeline stylesheet remains in the entry chunk.

Changes: Chat Timeline Loading

Layer / File(s) Summary
Timeline loader
src/components/chatTimelineLoader.ts
The loader shares an in-flight import, exposes the loaded component through an external store, and clears a failed import so a later call can retry.
ChatView integration
src/components/ChatView.tsx, src/components/ChatTimeline.tsx, src/AGENTS.md, changelog.d/lazy-chat-timeline.md
ChatView waits for the timeline during non-scrollback reads and renders turns when the component is available. It imports the timeline stylesheet in the entry chunk. The documentation and changelog describe the chunk loading and reported gzip size reduction.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested reviewers: kilhyeonjun

Merge Risk: ⚪ Minimal · up to 6a9a2

No merge-blocking issue is identified; the timeline may briefly show a loading state while its separate chunk loads.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 3 files. (2 skipped: 2… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: moving the chat timeline and LegendList into a separate chunk.
Description check Passed The description explains the problem, root cause, implementation, trade-offs, measurements, and validation results. It includes the required Change and Validation sections.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 3 files. (2 skipped: 2 unsupported.)

✨ Finishing Touches: 💡 2
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha
devswha merged commit a4b2295 into main Oct 13, 2026
15 of 17 checks passed
@devswha
devswha deleted the perf/lazy-chat-timeline branch October 13, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant