Skip to content

Commit d150fa1

Browse files
committed
Throttle reveals
1 parent fa27471 commit d150fa1

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetInlineCodeStrings.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-dom-bindings/src/server/fizz-instruction-set/ReactDOMFizzInstructionSetShared.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export function clientRenderBoundary(
4747
}
4848
}
4949

50+
const FALLBACK_THROTTLE_MS = 300;
51+
5052
export function completeBoundary(suspenseBoundaryID, contentID) {
5153
const contentNodeOuter = document.getElementById(contentID);
5254
if (!contentNodeOuter) {
@@ -69,6 +71,7 @@ export function completeBoundary(suspenseBoundaryID, contentID) {
6971
}
7072

7173
function revealCompletedBoundaries() {
74+
window['$RT'] = performance.now();
7275
const batch = window['$RB'];
7376
window['$RB'] = [];
7477
for (let i = 0; i < batch.length; i += 2) {
@@ -132,7 +135,18 @@ export function completeBoundary(suspenseBoundaryID, contentID) {
132135
// Queue this boundary for the next batch
133136
window['$RB'].push(suspenseIdNodeOuter, contentNodeOuter);
134137

135-
revealCompletedBoundaries();
138+
if (window['$RB'].length === 2) {
139+
// This is the first time we've pushed to the batch. We need to schedule a callback
140+
// to flush the batch. This is delayed by the throttle heuristic.
141+
const globalMostRecentFallbackTime =
142+
typeof window['$RT'] !== 'number' ? 0 : window['$RT'];
143+
const msUntilTimeout =
144+
globalMostRecentFallbackTime + FALLBACK_THROTTLE_MS - performance.now();
145+
// We always schedule the flush in a timer even if it's very low or negative to allow
146+
// for multiple completeBoundary calls that are already queued to have a chance to
147+
// make the batch.
148+
setTimeout(revealCompletedBoundaries, msUntilTimeout);
149+
}
136150
}
137151

138152
export function completeBoundaryWithStyles(

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ describe('ReactDOMFizzServer', () => {
209209
buffer = '';
210210

211211
if (!bufferedContent) {
212+
jest.runAllTimers();
212213
return;
213214
}
214215

@@ -317,6 +318,8 @@ describe('ReactDOMFizzServer', () => {
317318
div.innerHTML = bufferedContent;
318319
await insertNodesAndExecuteScripts(div, streamingContainer, CSPnonce);
319320
}
321+
// Let throttled boundaries reveal
322+
jest.runAllTimers();
320323
}
321324

322325
function resolveText(text) {

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ describe('ReactDOMFloat', () => {
125125
buffer = '';
126126

127127
if (!bufferedContent) {
128+
jest.runAllTimers();
128129
return;
129130
}
130131

@@ -233,6 +234,9 @@ describe('ReactDOMFloat', () => {
233234
div.innerHTML = bufferedContent;
234235
await insertNodesAndExecuteScripts(div, streamingContainer, CSPnonce);
235236
}
237+
await 0;
238+
// Let throttled boundaries reveal
239+
jest.runAllTimers();
236240
}
237241

238242
function getMeaningfulChildren(element) {
@@ -3612,6 +3616,7 @@ body {
36123616
assertConsoleErrorDev([
36133617
"Hydration failed because the server rendered HTML didn't match the client.",
36143618
]);
3619+
jest.runAllTimers();
36153620

36163621
expect(getMeaningfulChildren(document)).toEqual(
36173622
<html>
@@ -5205,6 +5210,10 @@ body {
52055210
</html>,
52065211
);
52075212
loadStylesheets();
5213+
// Let the styles flush and then flush the boundaries
5214+
await 0;
5215+
await 0;
5216+
jest.runAllTimers();
52085217
assertLog([
52095218
'load stylesheet: shell preinit/shell',
52105219
'load stylesheet: shell/shell preinit',

0 commit comments

Comments
 (0)