Skip to content

Commit

Permalink
Avoid appending Suspense boundary is the same reference is passed (#1154
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shuding committed Mar 14, 2024
1 parent 0bde030 commit b99b008
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-bottles-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix(ai/rsc): avoid appending boundary if the same reference was passed
6 changes: 6 additions & 0 deletions packages/core/rsc/streamable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export function createStreamableUI(initialValue?: React.ReactNode) {
update(value: React.ReactNode) {
assertStream('.update()');

// There is no need to update the value if it's referentially equal.
if (value === currentValue) {
warnUnclosedStream();
return;
}

const resolvable = createResolvablePromise();
currentValue = value;

Expand Down
22 changes: 22 additions & 0 deletions packages/core/rsc/streamable.ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,26 @@ describe('rsc - createStreamableUI()', () => {
'".update(): UI stream is already closed."',
);
});

it('should avoid sending data again if the same UI is passed', async () => {
const node = <div>1</div>;
const ui = createStreamableUI(node);
ui.update(node);
ui.update(node);
ui.update(node);
ui.update(node);
ui.update(node);
ui.update(node);
ui.done();

expect(await flightRender(ui.value)).toMatchInlineSnapshot(`
"1:\\"$Sreact.suspense\\"
2:D{\\"name\\":\\"\\",\\"env\\":\\"Server\\"}
0:[\\"$\\",\\"$1\\",null,{\\"fallback\\":[\\"$\\",\\"div\\",null,{\\"children\\":\\"1\\"}],\\"children\\":\\"$L2\\"}]
4:{\\"children\\":\\"1\\"}
3:[\\"$\\",\\"div\\",null,\\"$4\\"]
2:\\"$3\\"
"
`);
});
});

0 comments on commit b99b008

Please sign in to comment.