From 1db62223e08f8df2fca98aaa35f9320378864a8d Mon Sep 17 00:00:00 2001
From: Josh Story
Date: Wed, 18 Dec 2024 13:26:00 -0800
Subject: [PATCH] restore next14 sync base branch (#31841)
The original commit for nextjs 14 is
14898b6a93def197a1bbc6149192bb067fa6cc43 but since that was lost in the
facebook repo of React when the next nextjs-sync branch was created it
is being restored
---
.../ReactDOMLegacyServerStreamConfig.js | 6 --
.../src/server/ReactFizzConfigDOM.js | 7 +--
.../src/__tests__/ReactDOMFloat-test.js | 59 +++++++++++++++++++
.../src/ReactNoopFlightServer.js | 3 -
.../src/ReactServerStreamConfigBrowser.js | 29 ++-------
.../src/ReactServerStreamConfigBun.js | 6 --
.../src/ReactServerStreamConfigEdge.js | 29 ++-------
.../src/ReactServerStreamConfigFB.js | 6 --
.../src/ReactServerStreamConfigNode.js | 25 ++------
.../forks/ReactServerStreamConfig.custom.js | 1 -
10 files changed, 77 insertions(+), 94 deletions(-)
diff --git a/packages/react-dom-bindings/src/server/ReactDOMLegacyServerStreamConfig.js b/packages/react-dom-bindings/src/server/ReactDOMLegacyServerStreamConfig.js
index 0791791a14..5fa0c88d13 100644
--- a/packages/react-dom-bindings/src/server/ReactDOMLegacyServerStreamConfig.js
+++ b/packages/react-dom-bindings/src/server/ReactDOMLegacyServerStreamConfig.js
@@ -58,12 +58,6 @@ export function typedArrayToBinaryChunk(
throw new Error('Not implemented.');
}
-export function clonePrecomputedChunk(
- chunk: PrecomputedChunk,
-): PrecomputedChunk {
- return chunk;
-}
-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
throw new Error('Not implemented.');
}
diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
index 411e81180d..ac6c24a64b 100644
--- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
+++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
@@ -48,7 +48,6 @@ import {
writeChunkAndReturn,
stringToChunk,
stringToPrecomputedChunk,
- clonePrecomputedChunk,
} from 'react-server/src/ReactServerStreamConfig';
import {
resolveRequest,
@@ -4227,15 +4226,13 @@ export function writeCompletedBoundaryInstruction(
) {
resumableState.instructions |=
SentStyleInsertionFunction | SentCompleteBoundaryFunction;
- writeChunk(
- destination,
- clonePrecomputedChunk(completeBoundaryWithStylesScript1FullBoth),
- );
+ writeChunk(destination, completeBoundaryWithStylesScript1FullBoth);
} else if (
(resumableState.instructions & SentStyleInsertionFunction) ===
NothingSent
) {
resumableState.instructions |= SentStyleInsertionFunction;
+
writeChunk(destination, completeBoundaryWithStylesScript1FullPartial);
} else {
writeChunk(destination, completeBoundaryWithStylesScript1Partial);
diff --git a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js
index 31c1607cc1..7795060948 100644
--- a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js
+++ b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js
@@ -731,6 +731,65 @@ describe('ReactDOMFloat', () => {
).toEqual(['']);
});
+ // @gate enableFloat
+ it('can send style insertion implementation independent of boundary commpletion instruction implementation', async () => {
+ await act(() => {
+ renderToPipeableStream(
+
+
+
+ foo
+
+
+
+
+ bar
+
+
+
+ ,
+ ).pipe(writable);
+ });
+
+ expect(getMeaningfulChildren(document)).toEqual(
+
+
+
+ {'loading foo...'}
+ {'loading bar...'}
+
+ ,
+ );
+
+ await act(() => {
+ resolveText('foo');
+ });
+ expect(getMeaningfulChildren(document)).toEqual(
+
+
+ foo
+ {'loading bar...'}
+
+ ,
+ );
+ await act(() => {
+ resolveText('bar');
+ });
+ expect(getMeaningfulChildren(document)).toEqual(
+
+
+
+
+
+ foo
+ {'loading bar...'}
+
+
+ ,
+ );
+ });
+
// @gate enableFloat
it('can avoid inserting a late stylesheet if it already rendered on the client', async () => {
await act(() => {
diff --git a/packages/react-noop-renderer/src/ReactNoopFlightServer.js b/packages/react-noop-renderer/src/ReactNoopFlightServer.js
index 3d46f7694c..bb2dbafba2 100644
--- a/packages/react-noop-renderer/src/ReactNoopFlightServer.js
+++ b/packages/react-noop-renderer/src/ReactNoopFlightServer.js
@@ -46,9 +46,6 @@ const ReactNoopFlightServer = ReactFlightServer({
stringToPrecomputedChunk(content: string): Uint8Array {
return textEncoder.encode(content);
},
- clonePrecomputedChunk(chunk: Uint8Array): Uint8Array {
- return chunk;
- },
isClientReference(reference: Object): boolean {
return reference.$$typeof === Symbol.for('react.client.reference');
},
diff --git a/packages/react-server/src/ReactServerStreamConfigBrowser.js b/packages/react-server/src/ReactServerStreamConfigBrowser.js
index da1a56f813..f937130384 100644
--- a/packages/react-server/src/ReactServerStreamConfigBrowser.js
+++ b/packages/react-server/src/ReactServerStreamConfigBrowser.js
@@ -22,7 +22,7 @@ export function flushBuffered(destination: Destination) {
// transform streams. https://github.com/whatwg/streams/issues/960
}
-const VIEW_SIZE = 512;
+const VIEW_SIZE = 2048;
let currentView = null;
let writtenBytes = 0;
@@ -40,15 +40,6 @@ export function writeChunk(
}
if (chunk.byteLength > VIEW_SIZE) {
- if (__DEV__) {
- if (precomputedChunkSet.has(chunk)) {
- console.error(
- 'A large precomputed chunk was passed to writeChunk without being copied.' +
- ' Large chunks get enqueued directly and are not copied. This is incompatible with precomputed chunks because you cannot enqueue the same precomputed chunk twice.' +
- ' Use "cloneChunk" to make a copy of this large precomputed chunk before writing it. This is a bug in React.',
- );
- }
- }
// this chunk may overflow a single view which implies it was not
// one that is cached by the streaming renderer. We will enqueu
// it directly and expect it is not re-used
@@ -120,15 +111,15 @@ export function stringToChunk(content: string): Chunk {
return textEncoder.encode(content);
}
-const precomputedChunkSet: Set = __DEV__
- ? new Set()
- : (null: any);
-
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
const precomputedChunk = textEncoder.encode(content);
if (__DEV__) {
- precomputedChunkSet.add(precomputedChunk);
+ if (precomputedChunk.byteLength > VIEW_SIZE) {
+ console.error(
+ 'precomputed chunks must be smaller than the view size configured for this host. This is a bug in React.',
+ );
+ }
}
return precomputedChunk;
@@ -151,14 +142,6 @@ export function typedArrayToBinaryChunk(
return content.byteLength > VIEW_SIZE ? buffer.slice() : buffer;
}
-export function clonePrecomputedChunk(
- precomputedChunk: PrecomputedChunk,
-): PrecomputedChunk {
- return precomputedChunk.byteLength > VIEW_SIZE
- ? precomputedChunk.slice()
- : precomputedChunk;
-}
-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
return chunk.byteLength;
}
diff --git a/packages/react-server/src/ReactServerStreamConfigBun.js b/packages/react-server/src/ReactServerStreamConfigBun.js
index 276c7f59e4..ac8ae3f1a5 100644
--- a/packages/react-server/src/ReactServerStreamConfigBun.js
+++ b/packages/react-server/src/ReactServerStreamConfigBun.js
@@ -70,12 +70,6 @@ export function typedArrayToBinaryChunk(
return content;
}
-export function clonePrecomputedChunk(
- chunk: PrecomputedChunk,
-): PrecomputedChunk {
- return chunk;
-}
-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
return Buffer.byteLength(chunk, 'utf8');
}
diff --git a/packages/react-server/src/ReactServerStreamConfigEdge.js b/packages/react-server/src/ReactServerStreamConfigEdge.js
index 1bc9d7655a..e77dc28284 100644
--- a/packages/react-server/src/ReactServerStreamConfigEdge.js
+++ b/packages/react-server/src/ReactServerStreamConfigEdge.js
@@ -22,7 +22,7 @@ export function flushBuffered(destination: Destination) {
// transform streams. https://github.com/whatwg/streams/issues/960
}
-const VIEW_SIZE = 512;
+const VIEW_SIZE = 2048;
let currentView = null;
let writtenBytes = 0;
@@ -40,15 +40,6 @@ export function writeChunk(
}
if (chunk.byteLength > VIEW_SIZE) {
- if (__DEV__) {
- if (precomputedChunkSet.has(chunk)) {
- console.error(
- 'A large precomputed chunk was passed to writeChunk without being copied.' +
- ' Large chunks get enqueued directly and are not copied. This is incompatible with precomputed chunks because you cannot enqueue the same precomputed chunk twice.' +
- ' Use "cloneChunk" to make a copy of this large precomputed chunk before writing it. This is a bug in React.',
- );
- }
- }
// this chunk may overflow a single view which implies it was not
// one that is cached by the streaming renderer. We will enqueu
// it directly and expect it is not re-used
@@ -120,15 +111,15 @@ export function stringToChunk(content: string): Chunk {
return textEncoder.encode(content);
}
-const precomputedChunkSet: Set = __DEV__
- ? new Set()
- : (null: any);
-
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
const precomputedChunk = textEncoder.encode(content);
if (__DEV__) {
- precomputedChunkSet.add(precomputedChunk);
+ if (precomputedChunk.byteLength > VIEW_SIZE) {
+ console.error(
+ 'precomputed chunks must be smaller than the view size configured for this host. This is a bug in React.',
+ );
+ }
}
return precomputedChunk;
@@ -151,14 +142,6 @@ export function typedArrayToBinaryChunk(
return content.byteLength > VIEW_SIZE ? buffer.slice() : buffer;
}
-export function clonePrecomputedChunk(
- precomputedChunk: PrecomputedChunk,
-): PrecomputedChunk {
- return precomputedChunk.byteLength > VIEW_SIZE
- ? precomputedChunk.slice()
- : precomputedChunk;
-}
-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
return chunk.byteLength;
}
diff --git a/packages/react-server/src/ReactServerStreamConfigFB.js b/packages/react-server/src/ReactServerStreamConfigFB.js
index c763bde102..2257d0b3b7 100644
--- a/packages/react-server/src/ReactServerStreamConfigFB.js
+++ b/packages/react-server/src/ReactServerStreamConfigFB.js
@@ -60,12 +60,6 @@ export function typedArrayToBinaryChunk(
throw new Error('Not implemented.');
}
-export function clonePrecomputedChunk(
- chunk: PrecomputedChunk,
-): PrecomputedChunk {
- return chunk;
-}
-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
throw new Error('Not implemented.');
}
diff --git a/packages/react-server/src/ReactServerStreamConfigNode.js b/packages/react-server/src/ReactServerStreamConfigNode.js
index 5d5c6b4fab..cbd366ab54 100644
--- a/packages/react-server/src/ReactServerStreamConfigNode.js
+++ b/packages/react-server/src/ReactServerStreamConfigNode.js
@@ -99,15 +99,6 @@ function writeViewChunk(
return;
}
if (chunk.byteLength > VIEW_SIZE) {
- if (__DEV__) {
- if (precomputedChunkSet && precomputedChunkSet.has(chunk)) {
- console.error(
- 'A large precomputed chunk was passed to writeChunk without being copied.' +
- ' Large chunks get enqueued directly and are not copied. This is incompatible with precomputed chunks because you cannot enqueue the same precomputed chunk twice.' +
- ' Use "cloneChunk" to make a copy of this large precomputed chunk before writing it. This is a bug in React.',
- );
- }
- }
// this chunk may overflow a single view which implies it was not
// one that is cached by the streaming renderer. We will enqueu
// it directly and expect it is not re-used
@@ -201,14 +192,14 @@ export function stringToChunk(content: string): Chunk {
return content;
}
-const precomputedChunkSet = __DEV__ ? new Set() : null;
-
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
const precomputedChunk = textEncoder.encode(content);
if (__DEV__) {
- if (precomputedChunkSet) {
- precomputedChunkSet.add(precomputedChunk);
+ if (precomputedChunk.byteLength > VIEW_SIZE) {
+ console.error(
+ 'precomputed chunks must be smaller than the view size configured for this host. This is a bug in React.',
+ );
}
}
@@ -222,14 +213,6 @@ export function typedArrayToBinaryChunk(
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
}
-export function clonePrecomputedChunk(
- precomputedChunk: PrecomputedChunk,
-): PrecomputedChunk {
- return precomputedChunk.length > VIEW_SIZE
- ? precomputedChunk.slice()
- : precomputedChunk;
-}
-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
return typeof chunk === 'string'
? Buffer.byteLength(chunk, 'utf8')
diff --git a/packages/react-server/src/forks/ReactServerStreamConfig.custom.js b/packages/react-server/src/forks/ReactServerStreamConfig.custom.js
index cc92a88b01..e372e8903f 100644
--- a/packages/react-server/src/forks/ReactServerStreamConfig.custom.js
+++ b/packages/react-server/src/forks/ReactServerStreamConfig.custom.js
@@ -41,7 +41,6 @@ export const closeWithError = $$$config.closeWithError;
export const stringToChunk = $$$config.stringToChunk;
export const stringToPrecomputedChunk = $$$config.stringToPrecomputedChunk;
export const typedArrayToBinaryChunk = $$$config.typedArrayToBinaryChunk;
-export const clonePrecomputedChunk = $$$config.clonePrecomputedChunk;
export const byteLengthOfChunk = $$$config.byteLengthOfChunk;
export const byteLengthOfBinaryChunk = $$$config.byteLengthOfBinaryChunk;
export const createFastHash = $$$config.createFastHash;