Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🗜️ refactor: Eliminate Unstable React Keys During SSE Lifecycle (#12536)

Danny Avilab4d97bd88819
diff --git a/client/src/components/Chat/Messages/Message.tsx b/client/src/components/Chat/Messages/Message.tsx
index 53aef812fc..fc2a79fca0 100644
--- a/client/src/components/Chat/Messages/Message.tsx
+++ b/client/src/components/Chat/Messages/Message.tsx
@@ -47,7 +47,6 @@ export default function Message(props: TMessageProps) {
</div>
</MessageContainer>
<MultiMessage
- key={messageId}
messageId={messageId}
conversation={conversation}
messagesTree={children ?? []}
diff --git a/client/src/components/Chat/Messages/MessageIcon.tsx b/client/src/components/Chat/Messages/MessageIcon.tsx
index d63e0d2071..619d2f827d 100644
--- a/client/src/components/Chat/Messages/MessageIcon.tsx
+++ b/client/src/components/Chat/Messages/MessageIcon.tsx
@@ -1,4 +1,4 @@
-import { useMemo, memo } from 'react';
+import { useMemo, useEffect, useRef, memo } from 'react';
import { getEndpointField } from 'librechat-data-provider';
import type { Assistant, Agent } from 'librechat-data-provider';
import type { TMessageIcon } from '~/common';
@@ -19,38 +19,54 @@ type MessageIconProps = {
* this component renders display properties only, not identity-derived content.
*/
export function arePropsEqual(prev: MessageIconProps, next: MessageIconProps): boolean {
- if (prev.iconData?.endpoint !== next.iconData?.endpoint) {
- return false;
- }
- if (prev.iconData?.model !== next.iconData?.model) {
- return false;
- }
- if (prev.iconData?.iconURL !== next.iconData?.iconURL) {
- return false;
- }
- if (prev.iconData?.modelLabel !== next.iconData?.modelLabel) {
- return false;
- }
- if (prev.iconData?.isCreatedByUser !== next.iconData?.isCreatedByUser) {
- return false;
- }
- if (prev.agent?.name !== next.agent?.name) {
- return false;
- }
- if (prev.agent?.avatar?.filepath !== next.agent?.avatar?.filepath) {
- return false;
- }
- if (prev.assistant?.name !== next.assistant?.name) {
- return false;
- }
- if (prev.assistant?.metadata?.avatar !== next.assistant?.metadata?.avatar) {
- return false;
+ const checks: [string, unknown, unknown][] = [
+ ['iconData.endpoint', prev.iconData?.endpoint, next.iconData?.endpoint],
+ ['iconData.model', prev.iconData?.model, next.iconData?.model],
+ ['iconData.iconURL', prev.iconData?.iconURL, next.iconData?.iconURL],
+ ['iconData.modelLabel', prev.iconData?.modelLabel, next.iconData?.modelLabel],
+ ['iconData.isCreatedByUser', prev.iconData?.isCreatedByUser, next.iconData?.isCreatedByUser],
+ ['agent.name', prev.agent?.name, next.agent?.name],
+ ['agent.avatar.filepath', prev.agent?.avatar?.filepath, next.agent?.avatar?.filepath],
+ ['assistant.name', prev.assistant?.name, next.assistant?.name],
+ [
+ 'assistant.metadata.avatar',
+ prev.assistant?.metadata?.avatar,
+ next.assistant?.metadata?.avatar,
+ ],
+ ];
+
+ for (const [field, prevVal, nextVal] of checks) {
+ if (prevVal !== nextVal) {
+ logger.log('icon_memo_diff', `field "${field}" changed:`, prevVal, '→', nextVal);
+ return false;
+ }
}
return true;
}
const MessageIcon = memo(({ iconData, assistant, agent }: MessageIconProps) => {
- logger.log('icon_data', iconData, assistant, agent);
+ const renderCountRef = useRef(0);
+ renderCountRef.current += 1;
+
+ useEffect(() => {
+ logger.log(
+ 'icon_lifecycle',
+ 'MOUNT',
+ iconData?.modelLabel,
+ `render #${renderCountRef.current}`,
+ );
+ return () => {
+ logger.log('icon_lifecycle', 'UNMOUNT', iconData?.modelLabel);
+ };
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
+ logger.log(
+ 'icon_data',
+ `render #${renderCountRef.current}`,
+ iconData?.isCreatedByUser ? 'user' : iconData?.modelLabel,
+ iconData,
+ );
const { data: endpointsConfig } = useGetEndpointsQuery();
const agentName = agent?.name ?? '';
diff --git a/client/src/components/Chat/Messages/MessageParts.tsx b/client/src/components/Chat/Messages/MessageParts.tsx
index 3d13fa6ae0..2d0e3d512b 100644
--- a/client/src/components/Chat/Messages/MessageParts.tsx
+++ b/client/src/components/Chat/Messages/MessageParts.tsx
@@ -179,7 +179,6 @@ export default function Message(props: TMessageProps) {
</div>
</div>
<MultiMessage
- key={messageId}
messageId={messageId}
conversation={conversation}
messagesTree={children ?? []}
diff --git a/client/src/components/Chat/Messages/MessagesView.tsx b/client/src/components/Chat/Messages/MessagesView.tsx
index ca4a0cce85..b4f555d189 100644
--- a/client/src/components/Chat/Messages/MessagesView.tsx
+++ b/client/src/components/Chat/Messages/MessagesView.tsx
@@ -62,7 +62,6 @@ function MessagesViewContent({
<>
<div ref={screenshotTargetRef}>
<MultiMessage
- key={conversationId}
messagesTree={_messagesTree}
messageId={conversationId ?? null}
setCurrentEditId={setCurrentEditId}
diff --git a/client/src/components/Chat/Messages/MultiMessage.tsx b/client/src/components/Chat/Messages/MultiMessage.tsx
index 13eb2d143f..4a561045f5 100644
--- a/client/src/components/Chat/Messages/MultiMessage.tsx
+++ b/client/src/components/Chat/Messages/MultiMessage.tsx
@@ -39,47 +39,40 @@ export default function MultiMessage({
return null;
}
- const message = messagesTree[messagesTree.length - siblingIdx - 1] as TMessage | undefined;
+ const currentSiblingIdx = messagesTree.length - siblingIdx - 1;
+ const message = messagesTree[currentSiblingIdx] as TMessage | undefined;
if (!message) {
return null;
}
+ /**
+ * No explicit key — React uses positional reconciliation since MultiMessage
+ * always renders exactly one child at this position.
+ *
+ * Both messageId and parentMessageId change during the SSE lifecycle
+ * (client UUID → createdHandler ID → server ID), so neither can serve as a
+ * stable key. Using either caused React to unmount/remount the entire subtree
+ * on each SSE event, destroying memoized state and causing visible flickering.
+ *
+ * Without a key, React reuses the component instance and updates props in place.
+ * The memo comparators on ContentRender/MessageRender handle field-level diffing,
+ * and sibling switches work correctly because the message prop changes entirely.
+ */
+ const sharedProps = {
+ message,
+ currentEditId,
+ setCurrentEditId,
+ siblingIdx: currentSiblingIdx,
+ siblingCount: messagesTree.length,
+ setSiblingIdx: setSiblingIdxRev,
+ };
+
if (isAssistantsEndpoint(message.endpoint) && message.content) {
- return (
- <MessageParts
- key={message.messageId}
- message={message}
- currentEditId={currentEditId}
- setCurrentEditId={setCurrentEditId}
- siblingIdx={messagesTree.length - siblingIdx - 1}
- siblingCount={messagesTree.length}
- setSiblingIdx={setSiblingIdxRev}
- />
- );
+ return <MessageParts {...sharedProps} />;
} else if (message.content) {
- return (
- <MessageContent
- key={message.messageId}
- message={message}
- currentEditId={currentEditId}
- setCurrentEditId={setCurrentEditId}
- siblingIdx={messagesTree.length - siblingIdx - 1}
- siblingCount={messagesTree.length}
- setSiblingIdx={setSiblingIdxRev}
- />
- );
+ return <MessageContent {...sharedProps} />;
}
- return (
- <Message
- key={message.messageId}
- message={message}
- currentEditId={currentEditId}
- setCurrentEditId={setCurrentEditId}
- siblingIdx={messagesTree.length - siblingIdx - 1}
- siblingCount={messagesTree.length}
- setSiblingIdx={setSiblingIdxRev}
- />
- );
+ return <Message {...sharedProps} />;
}
diff --git a/client/src/components/Chat/Messages/__tests__/MessageIcon.render.test.tsx b/client/src/components/Chat/Messages/__tests__/MessageIcon.render.test.tsx
new file mode 100644
index 0000000000..1c7b3c1aec
--- /dev/null
+++ b/client/src/components/Chat/Messages/__tests__/MessageIcon.render.test.tsx
@@ -0,0 +1,193 @@
+import React from 'react';
+import { render } from '@testing-library/react';
+import { EModelEndpoint } from 'librechat-data-provider';
+import type { Agent } from 'librechat-data-provider';
+import type { TMessageIcon } from '~/common';
+
+jest.mock('librechat-data-provider', () => ({
+ ...jest.requireActual('librechat-data-provider'),
+ getEndpointField: jest.fn(() => ''),
+}));
+jest.mock('~/data-provider', () => ({
+ useGetEndpointsQuery: jest.fn(() => ({ data: {} })),
+}));
+
+// logger is a plain object with a real function — not a jest.fn() —
+// so restoreMocks/clearMocks won't touch it. We spy on it per-test instead.
+const logCalls: unknown[][] = [];
+jest.mock('~/utils', () => ({
+ getIconEndpoint: jest.fn(() => 'agents'),
+ logger: {
+ log: (...args: unknown[]) => {
+ logCalls.push(args);
+ },
+ },
+}));
+jest.mock('~/components/Endpoints/ConvoIconURL', () => {
+ const ConvoIconURL = (props: Record<string, unknown>) => (
+ <div data-testid="convo-icon-url" data-icon-url={props.iconURL as string} />
+ );
+ ConvoIconURL.displayName = 'ConvoIconURL';
+ return { __esModule: true, default: ConvoIconURL };
+});
+jest.mock('~/components/Endpoints/Icon', () => {
+ const Icon = (props: Record<string, unknown>) => (
+ <div data-testid="icon" data-icon-url={props.iconURL as string} />
+ );
+ Icon.displayName = 'Icon';
+ return { __esModule: true, default: Icon };
+});
+
+import MessageIcon from '../MessageIcon';
+
+const makeAgent = (overrides?: Partial<Agent>): Agent =>
+ ({
+ id: 'agent_123',
+ name: 'GitHub Agent',
+ avatar: { filepath: '/images/agent-avatar.png' },
+ ...overrides,
+ }) as Agent;
+
+const baseIconData: TMessageIcon = {
+ endpoint: EModelEndpoint.agents,
+ model: 'agent_123',
+ iconURL: undefined,
+ modelLabel: 'GitHub Agent',
+ isCreatedByUser: false,
+};
+
+describe('MessageIcon render cycles', () => {
+ beforeEach(() => {
+ logCalls.length = 0;
+ });
+
+ it('renders once on initial mount', () => {
+ render(<MessageIcon iconData={baseIconData} agent={makeAgent()} />);
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(1);
+ });
+
+ it('does not re-render when parent re-renders with same field values but new object references', () => {
+ const agent = makeAgent();
+ const { rerender } = render(<MessageIcon iconData={baseIconData} agent={agent} />);
+ logCalls.length = 0;
+
+ // Simulate parent re-render: new iconData object (same field values), new agent object (same data)
+ rerender(<MessageIcon iconData={{ ...baseIconData }} agent={makeAgent()} />);
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(0);
+ });
+
+ it('does not re-render when agent object reference changes but name and avatar are the same', () => {
+ const agent1 = makeAgent();
+ const { rerender } = render(<MessageIcon iconData={baseIconData} agent={agent1} />);
+ logCalls.length = 0;
+
+ // New agent object with different id but same display fields
+ const agent2 = makeAgent({ id: 'agent_456' });
+ rerender(<MessageIcon iconData={baseIconData} agent={agent2} />);
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(0);
+ });
+
+ it('re-renders when agent avatar filepath changes', () => {
+ const agent1 = makeAgent();
+ const { rerender } = render(<MessageIcon iconData={baseIconData} agent={agent1} />);
+ logCalls.length = 0;
+
+ const agent2 = makeAgent({ avatar: { filepath: '/images/new-avatar.png' } });
+ rerender(<MessageIcon iconData={baseIconData} agent={agent2} />);
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(1);
+ });
+
+ it('re-renders when agent goes from undefined to defined (name changes from undefined to string)', () => {
+ const { rerender } = render(<MessageIcon iconData={baseIconData} agent={undefined} />);
+ logCalls.length = 0;
+
+ rerender(<MessageIcon iconData={baseIconData} agent={makeAgent()} />);
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(1);
+ });
+
+ describe('simulates message lifecycle', () => {
+ it('renders exactly twice during new message + streaming start: initial render + modelLabel update', () => {
+ // Phase 1: Initial response message created by useChatFunctions
+ // model is set to agent_id, iconURL is undefined, modelLabel is '' or sender
+ const initialIconData: TMessageIcon = {
+ endpoint: EModelEndpoint.agents,
+ model: 'agent_123',
+ iconURL: undefined,
+ modelLabel: '', // Not yet resolved
+ isCreatedByUser: false,
+ };
+ const agent = makeAgent();
+
+ const { rerender } = render(<MessageIcon iconData={initialIconData} agent={agent} />);
+
+ // Phase 2: First streaming chunk arrives, messageLabel resolves to agent name
+ // This is a legitimate re-render — modelLabel changed from '' to 'GitHub Agent'
+ const streamingIconData: TMessageIcon = {
+ ...initialIconData,
+ modelLabel: 'GitHub Agent',
+ };
+
+ rerender(<MessageIcon iconData={streamingIconData} agent={agent} />);
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ // Exactly 2: initial mount + modelLabel change
+ expect(iconDataCalls).toHaveLength(2);
+ });
+
+ it('does NOT re-render on subsequent streaming chunks (content changes, isSubmitting stays true)', () => {
+ const iconData: TMessageIcon = {
+ endpoint: EModelEndpoint.agents,
+ model: 'agent_123',
+ iconURL: undefined,
+ modelLabel: 'GitHub Agent',
+ isCreatedByUser: false,
+ };
+ const agent = makeAgent();
+
+ const { rerender } = render(<MessageIcon iconData={iconData} agent={agent} />);
+ logCalls.length = 0;
+
+ // Simulate multiple parent re-renders from streaming chunks
+ // Parent (ContentRender) re-renders because chatContext changed,
+ // but MessageIcon props are identical field-by-field
+ for (let i = 0; i < 5; i++) {
+ rerender(<MessageIcon iconData={{ ...iconData }} agent={makeAgent()} />);
+ }
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(0);
+ });
+
+ it('does NOT re-render when agentsMap context updates with same agent data', () => {
+ const iconData: TMessageIcon = {
+ endpoint: EModelEndpoint.agents,
+ model: 'agent_123',
+ iconURL: undefined,
+ modelLabel: 'GitHub Agent',
+ isCreatedByUser: false,
+ };
+
+ // First render with agent from original agentsMap
+ const agent1 = makeAgent();
+ const { rerender } = render(<MessageIcon iconData={iconData} agent={agent1} />);
+ logCalls.length = 0;
+
+ // agentsMap refetched → new agent object, same display data
+ const agent2 = makeAgent();
+ expect(agent1).not.toBe(agent2); // different reference
+ rerender(<MessageIcon iconData={iconData} agent={agent2} />);
+
+ const iconDataCalls = logCalls.filter((c) => c[0] === 'icon_data');
+ expect(iconDataCalls).toHaveLength(0);
+ });
+ });
+});
diff --git a/client/src/components/Messages/MessageContent.tsx b/client/src/components/Messages/MessageContent.tsx
index 977e397022..67865ed397 100644
--- a/client/src/components/Messages/MessageContent.tsx
+++ b/client/src/components/Messages/MessageContent.tsx
@@ -48,7 +48,6 @@ export default function MessageContent(props: TMessageProps) {
</div>
</MessageContainer>
<MultiMessage
- key={messageId}
messageId={messageId}
conversation={conversation}
messagesTree={children ?? []}