Skip to content

Commit b99b008

Browse files
authoredMar 14, 2024
Avoid appending Suspense boundary is the same reference is passed (#1154)
1 parent 0bde030 commit b99b008

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
 

Diff for: ‎.changeset/clever-bottles-do.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix(ai/rsc): avoid appending boundary if the same reference was passed

Diff for: ‎packages/core/rsc/streamable.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export function createStreamableUI(initialValue?: React.ReactNode) {
5252
update(value: React.ReactNode) {
5353
assertStream('.update()');
5454

55+
// There is no need to update the value if it's referentially equal.
56+
if (value === currentValue) {
57+
warnUnclosedStream();
58+
return;
59+
}
60+
5561
const resolvable = createResolvablePromise();
5662
currentValue = value;
5763

Diff for: ‎packages/core/rsc/streamable.ui.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,26 @@ describe('rsc - createStreamableUI()', () => {
423423
'".update(): UI stream is already closed."',
424424
);
425425
});
426+
427+
it('should avoid sending data again if the same UI is passed', async () => {
428+
const node = <div>1</div>;
429+
const ui = createStreamableUI(node);
430+
ui.update(node);
431+
ui.update(node);
432+
ui.update(node);
433+
ui.update(node);
434+
ui.update(node);
435+
ui.update(node);
436+
ui.done();
437+
438+
expect(await flightRender(ui.value)).toMatchInlineSnapshot(`
439+
"1:\\"$Sreact.suspense\\"
440+
2:D{\\"name\\":\\"\\",\\"env\\":\\"Server\\"}
441+
0:[\\"$\\",\\"$1\\",null,{\\"fallback\\":[\\"$\\",\\"div\\",null,{\\"children\\":\\"1\\"}],\\"children\\":\\"$L2\\"}]
442+
4:{\\"children\\":\\"1\\"}
443+
3:[\\"$\\",\\"div\\",null,\\"$4\\"]
444+
2:\\"$3\\"
445+
"
446+
`);
447+
});
426448
});

0 commit comments

Comments
 (0)
Please sign in to comment.