@devries-cameron20 / t3code-1 / commits / cb9629e689
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): thread switches skip re-parsing, sidebar scrolling, and composer layout reads (#27823)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 30, 2026 at 14:54 UTC cb9629e6891ef0745772b69f09c9acd0c6a74f7e
6 files changed +102 -4
apps/web/src/components/ChatMarkdown.browser.test.tsx
+26
@@ -16,6 +16,18 @@ const { workspaceSearch, filesystemMetadata } = vi.hoisted(() => ({
16 workspaceSearch: vi.fn(),
17 filesystemMetadata: vi.fn(),
18 }));
19 +const parsedSources = vi.hoisted(() => [] as string[]);
20 +vi.mock(import("mdast-util-from-markdown"), async (original) => {
21 + const actual = await original();
22 + return {
23 + ...actual,
24 + fromMarkdown: ((source: string, ...rest: never[]) => {
25 + parsedSources.push(String(source));
26 + return (actual.fromMarkdown as (...args: unknown[]) => unknown)(source, ...rest);
27 + }) as typeof actual.fromMarkdown,
28 + };
29 +});
30 +
31 vi.mock(import("../state/use-atom-query-runner"), async (original) => ({
32 ...(await original()),
33 useAtomQueryRunner: ((family: unknown) =>
@@ -99,6 +111,7 @@ it("keeps compact audio playback loaded across signed URL renewals", async () =>
111 });
112
113 function setup() {
114 + parsedSources.length = 0;
115 settingsOverrides.mathRenderingEnabled = false;
116 renderDiagram.mockReset();
117 workspaceSearch.mockReset().mockResolvedValue(AsyncResult.success({ entries: [] }));
@@ -139,6 +152,19 @@ it("loads a host image through the page's own route when there is no environment
152 );
153 });
154
155 +it("reuses the parse of a streaming reply's finished blocks when it remounts", async () => {
156 + setup();
157 + const message = (
158 + <ChatMarkdown cwd="/tmp/project" text={"Finished block\n\nStill growing"} isStreaming />
159 + );
160 + {
161 + await using _first = await render(message);
162 + }
163 + await using screen = await render(message);
164 + await expect.element(screen.getByText("Still growing")).toBeVisible();
165 + expect(parsedSources.filter((source) => source.includes("Finished block"))).toHaveLength(1);
166 +});
167 +
168 describe("ChatMarkdown favicon privacy", () => {
169 it("suppresses private link images while preserving public links across updates", async () => {
170 setup();
apps/web/src/components/ChatMarkdown.tsx
+2 -1
@@ -1767,7 +1767,8 @@ function ChatMarkdown({
1767 : CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS
1768 }
1769 shared={!uncachedPlugins}
1770 - cache={!props.isStreaming}
1770 + // Every block before the last is finished, so its parse can be reused.
1771 + cache={!props.isStreaming || index < blocks.length - 1}
1772 />
1773 ))}
1774 </ChatMarkdownRendererContext>
apps/web/src/components/chat/useComposerMultilinePrompt.browser.test.tsx new
+23
@@ -0,0 +1,23 @@
1 +import { expect, it } from "vite-plus/test";
2 +
3 +import { render } from "../../../test/render";
4 +import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt";
5 +
6 +function Composer({ text }: { readonly text: string }) {
7 + const [multiline, observeBody] = useComposerMultilinePrompt();
8 + return (
9 + <div ref={observeBody} style={{ width: 200, lineHeight: "20px" }}>
10 + <div data-testid="composer-editor" contentEditable suppressContentEditableWarning>
11 + {text}
12 + </div>
13 + <output>{multiline ? "multiline" : "single line"}</output>
14 + </div>
15 + );
16 +}
17 +
18 +it("notices when typing makes the prompt wrap", async () => {
19 + await using screen = await render(<Composer text="short" />, { router: false });
20 + await expect.element(screen.getByText("single line")).toBeVisible();
21 + await screen.rerender(<Composer text={"a long prompt that wraps ".repeat(6)} />);
22 + await expect.element(screen.getByText("multiline")).toBeVisible();
23 +});
apps/web/src/components/chat/useComposerMultilinePrompt.ts
+10 -2
@@ -27,9 +27,16 @@ export function useComposerMultilinePrompt(): readonly [
27 const [isMultiline, setIsMultiline] = useState(false);
28 const observeBody = (body: HTMLElement | null) => {
29 if (!body) return;
30 + // Measuring reads layout, so one measurement per frame runs after all of the frame's DOM
31 + // writes instead of forcing a layout after each mutation.
32 + let frame: number | null = null;
33 const measure = () => {
31 - const next = measureComposerMultilinePrompt(body);
32 - if (next !== null) setIsMultiline(next);
34 + if (frame !== null) return;
35 + frame = requestAnimationFrame(() => {
36 + frame = null;
37 + const next = measureComposerMultilinePrompt(body);
38 + if (next !== null) setIsMultiline(next);
39 + });
40 };
41 measure();
42 const editor = body.querySelector<HTMLElement>('[data-testid="composer-editor"]');
@@ -37,6 +44,7 @@ export function useComposerMultilinePrompt(): readonly [
44 const mutationObserver = new MutationObserver(measure);
45 mutationObserver.observe(body, { childList: true, characterData: true, subtree: true });
46 return () => {
47 + if (frame !== null) cancelAnimationFrame(frame);
48 stopObserving();
49 mutationObserver.disconnect();
50 };
apps/web/src/components/sidebar/routeReveal.browser.test.tsx
+38 -1
@@ -1,4 +1,5 @@
1 -import { describe, expect, it } from "vite-plus/test";
1 +import { describe, expect, it, vi } from "vite-plus/test";
2 +import { userEvent } from "vite-plus/test/browser";
3
4 import { render } from "../../../test/render";
5 import { type SidebarRouteReveal, useActiveRowReveal } from "./routeReveal";
@@ -17,6 +18,7 @@ function RevealList(props: {
18 <li
19 key={key}
20 ref={key === props.activeKey ? (row) => revealActiveRow(row, key) : undefined}
21 + tabIndex={-1}
22 style={{ height: ROW_HEIGHT_PX }}
23 >
24 {key}
@@ -46,6 +48,41 @@ describe("useActiveRowReveal", () => {
48 await expect.element(target).toBeInViewport();
49 });
50
51 + it("leaves the list where it is when the user clicked the row that opened the thread", async () => {
52 + const reveal = newReveal();
53 + const keys = rowKeys(50);
54 + await using screen = await render(
55 + <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
56 + // No router: the route arrives as the activeKey prop.
57 + { router: false },
58 + );
59 + await userEvent.hover(screen.getByText("thread-2", { exact: true }));
60 + // Even a no-op scroll forces a layout of the thread being opened.
61 + using scrollIntoView = vi.spyOn(Element.prototype, "scrollIntoView");
62 +
63 + await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-2" />);
64 +
65 + await vi.waitFor(() => expect(reveal.pendingKey).toBeNull());
66 + expect(scrollIntoView).not.toHaveBeenCalled();
67 + });
68 +
69 + it("reveals a focused row scrolled out of view when the keyboard opens its thread", async () => {
70 + const reveal = newReveal();
71 + const keys = rowKeys(50);
72 + await using screen = await render(
73 + <RevealList reveal={reveal} rowKeys={keys} activeKey="thread-0" />,
74 + // No router: the route arrives as the activeKey prop.
75 + { router: false },
76 + );
77 + const target = screen.getByText("thread-40", { exact: true });
78 + target.element().focus({ preventScroll: true });
79 + await expect.element(target).not.toBeInViewport();
80 +
81 + await screen.rerender(<RevealList reveal={reveal} rowKeys={keys} activeKey="thread-40" />);
82 +
83 + await expect.element(target).toBeInViewport();
84 + });
85 +
86 it("reveals the active row when it mounts after the shells arrive", async () => {
87 const reveal = newReveal();
88 // The route already names the thread, but its environment's shells have not
apps/web/src/components/sidebar/routeReveal.ts
+3
@@ -38,6 +38,9 @@ export function useActiveRowReveal(reveal: SidebarRouteReveal, routeKey: string
38 if (reveal.pendingKey !== rowKey && !mountRevealOwed.current) return;
39 completeSidebarRouteReveal(reveal);
40 mountRevealOwed.current = false;
41 + // A row under the pointer was just clicked, so it is in view; scrolling it would force a
42 + // layout mid-commit. Keyboard activation can come from a focused row scrolled out of view.
43 + if (row.matches(":hover")) return;
44 row.scrollIntoView({ block: "nearest" });
45 };
46 }