GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(web): completed Markdown blocks no longer render twice · pingdotgg/t3code@684c3ab

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/web/src/components
    • ChatMarkdown.browser.test.tsx
    • ChatMarkdown.tsx
    • chat
      • markdownRenderer.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Copy file name to clipboardExpand all lines: apps/web/src/components/ChatMarkdown.browser.test.tsx
+22-2Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ it("reuses the parse of a streaming reply's finished blocks when it remounts", a
219219
<ChatMarkdown cwd="/tmp/project" text={"Finished block\n\nStill growing"} isStreaming />
220220
);
221221
{
222-
await using _first = await render(message);
222+
await using first = await render(
223+
<ChatMarkdown cwd="/tmp/project" text={"Finished block\n\n"} isStreaming />,
224+
);
225+
await first.rerender(message);
223226
}
224227
await using screen = await render(message);
225228
await expect.element(screen.getByText("Still growing")).toBeVisible();
@@ -234,11 +237,28 @@ it("reuses the parse of a remounted message that has stable extra plugins", asyn
234237
<ChatMarkdown cwd="/tmp/project" text="Cached text" extraRemarkPlugins={countingPlugins} />
235238
);
236239
{
237-
await using _first = await render(message);
240+
await using first = await render(
241+
<ChatMarkdown
242+
cwd="/tmp/project"
243+
text="Cached text"
244+
extraRemarkPlugins={countingPlugins}
245+
isStreaming
246+
/>,
247+
);
248+
await first.rerender(message);
238249
}
239250
await using screen = await render(message);
240251
await expect.element(screen.getByText("Cached text")).toBeVisible();
241252
expect(parsed).toHaveBeenCalledTimes(1);
253+
const changed = vi.fn();
254+
await screen.rerender(
255+
<ChatMarkdown
256+
cwd="/tmp/project"
257+
text="Cached text"
258+
extraRemarkPlugins={[() => () => void changed()]}
259+
/>,
260+
);
261+
expect(changed).toHaveBeenCalledTimes(1);
242262
});
243263

244264
describe("ChatMarkdown favicon privacy", () => {
Collapse file

‎apps/web/src/components/ChatMarkdown.tsx‎

Copy file name to clipboardExpand all lines: apps/web/src/components/ChatMarkdown.tsx
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import React, {
7878
type MouseEvent as ReactMouseEvent,
7979
use,
8080
memo,
81+
useEffect,
8182
useLayoutEffect,
8283
useRef,
8384
useState,
@@ -1720,9 +1721,12 @@ const MarkdownSource = memo(function MarkdownSource({
17201721
components: CHAT_MARKDOWN_COMPONENTS,
17211722
urlTransform: markdownUrlTransform,
17221723
});
1723-
return (
1724-
<ChatMarkdownTextContext value={text}>{render(text, shared && cache)}</ChatMarkdownTextContext>
1725-
);
1724+
const element = render(text, false);
1725+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Promote the committed element into the shared parse cache when its block finishes.
1726+
useEffect(() => {
1727+
if (shared && cache) render.remember(text, element);
1728+
}, [render, text, element, shared, cache]);
1729+
return <ChatMarkdownTextContext value={text}>{element}</ChatMarkdownTextContext>;
17261730
});
17271731

17281732
function ChatMarkdown({
Collapse file

‎apps/web/src/components/chat/markdownRenderer.ts‎

Copy file name to clipboardExpand all lines: apps/web/src/components/chat/markdownRenderer.ts
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function createMarkdownRenderer({
8282
}
8383
};
8484

85-
return (text: string, cache: boolean): ReactElement => {
85+
const render = (text: string, cache: boolean): ReactElement => {
8686
const key = keyPrefix + text;
8787
const cached = rendered.get(key);
8888
if (cached !== undefined) {
@@ -106,4 +106,11 @@ export function createMarkdownRenderer({
106106
if (cache) remember(key, element);
107107
return element;
108108
};
109+
110+
return Object.assign(render, {
111+
remember(text: string, element: ReactElement) {
112+
const key = keyPrefix + text;
113+
if (!rendered.has(key)) remember(key, element);
114+
},
115+
});
109116
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.