Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-code-blocks-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"streamdown": patch
---

Fix streamed code blocks remaining stale when their content changes without moving in the document.
24 changes: 24 additions & 0 deletions packages/streamdown/__tests__/code-block-memo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ vi.mock("../lib/code-block", () => ({
}));

describe("MemoCode in streaming mode", () => {
it("should re-render code blocks when their content changes in place", async () => {
codeBlockRenderCount = 0;

const initial = "```js\nfirst\n```";
const updated = "```js\nsecond\n```";

const { container, rerender } = render(<Streamdown>{initial}</Streamdown>);

await waitFor(() => {
expect(container.textContent).toContain("first");
});

const initialRenderCount = codeBlockRenderCount;

rerender(<Streamdown>{updated}</Streamdown>);

await waitFor(() => {
expect(container.textContent).toContain("second");
});

expect(container.textContent).not.toContain("first");
expect(codeBlockRenderCount).toBeGreaterThan(initialRenderCount);
});

it("should not re-render unchanged code blocks on streaming updates", async () => {
codeBlockRenderCount = 0;

Expand Down
51 changes: 37 additions & 14 deletions packages/streamdown/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ function sameClassAndNode(
);
}

function getCodeContent(children: React.ReactNode): string | undefined {
if (typeof children === "string") {
return children;
}

if (
isValidElement(children) &&
children.props &&
typeof children.props === "object" &&
"children" in children.props &&
typeof children.props.children === "string"
) {
return children.props.children;
}

return undefined;
}

function sameCodeContent(
prev: React.ReactNode,
next: React.ReactNode
): boolean {
const prevContent = getCodeContent(prev);
const nextContent = getCodeContent(next);

if (prevContent === undefined && nextContent === undefined) {
return prev === next;
}

return prevContent === nextContent;
}

const shouldShowControls = (
config: ControlsConfig,
type: "table" | "code" | "mermaid" | "image"
Expand Down Expand Up @@ -852,19 +884,7 @@ const CodeComponent = ({
: false;
const showLineNumbers = !metaNoLineNumbers && contextLineNumbers !== false;

// Extract code content from children safely
let code = "";
if (
isValidElement(children) &&
children.props &&
typeof children.props === "object" &&
"children" in children.props &&
typeof children.props.children === "string"
) {
code = children.props.children;
} else if (typeof children === "string") {
code = children;
}
const code = getCodeContent(children) ?? "";

if (customRenderer) {
const CustomComponent = customRenderer.component;
Expand Down Expand Up @@ -981,7 +1001,10 @@ const MemoCode = memo<
DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> & ExtraProps
>(
CodeComponent,
(p, n) => p.className === n.className && sameNodePosition(p.node, n.node)
(p, n) =>
p.className === n.className &&
sameNodePosition(p.node, n.node) &&
sameCodeContent(p.children, n.children)
);
MemoCode.displayName = "MarkdownCode";

Expand Down