Skip to content

Commit b0e511c

Browse files
committed
[Flight] Use Web Streams APIs for 3rd-party component in Flight fixture
Now that the Node bundles for Webpack also expose the Web Streams APIs, we can can use those in the Flight fixture for caching the 3rd-party component, without needing to convert the streams back and forth. This new approach of how we're exposing those APIs also allows us to mix and match them with the Node Streams APIs (e.g. in the outer rendering in `fixtures/flight/server/region.js`). This was not possibly with the previous approach of using separate bundles, because React only allows a single RSC renderer to be active at a time.
1 parent e4b88ae commit b0e511c

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

fixtures/flight/src/App.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
2-
import {renderToPipeableStream} from 'react-server-dom-webpack/server';
3-
import {createFromNodeStream} from 'react-server-dom-webpack/client';
2+
import {renderToReadableStream} from 'react-server-dom-webpack/server';
3+
import {createFromReadableStream} from 'react-server-dom-webpack/client';
44
import {PassThrough, Readable} from 'stream';
55

66
import Container from './Container.js';
@@ -46,43 +46,33 @@ async function ThirdPartyComponent() {
4646
return delay('hello from a 3rd party', 30);
4747
}
4848

49-
// Using Web streams for tee'ing convenience here.
50-
let cachedThirdPartyReadableWeb;
49+
let cachedThirdPartyStream;
5150

5251
// We create the Component outside of AsyncLocalStorage so that it has no owner.
5352
// That way it gets the owner from the call to createFromNodeStream.
5453
const thirdPartyComponent = <ThirdPartyComponent />;
5554

5655
function fetchThirdParty(noCache) {
57-
if (cachedThirdPartyReadableWeb && !noCache) {
58-
const [readableWeb1, readableWeb2] = cachedThirdPartyReadableWeb.tee();
59-
cachedThirdPartyReadableWeb = readableWeb1;
60-
61-
return createFromNodeStream(Readable.fromWeb(readableWeb2), {
56+
// We're using the Web Streams APIs for tee'ing convenience.
57+
const stream =
58+
cachedThirdPartyStream && !noCache
59+
? cachedThirdPartyStream
60+
: renderToReadableStream(
61+
thirdPartyComponent,
62+
{},
63+
{environmentName: 'third-party'}
64+
);
65+
66+
const [stream1, stream2] = stream.tee();
67+
cachedThirdPartyStream = stream1;
68+
69+
return createFromReadableStream(stream2, {
70+
serverConsumerManifest: {
6271
moduleMap: {},
63-
moduleLoading: {},
64-
});
65-
}
66-
67-
const stream = renderToPipeableStream(
68-
thirdPartyComponent,
69-
{},
70-
{environmentName: 'third-party'}
71-
);
72-
73-
const readable = new PassThrough();
74-
// React currently only supports piping to one stream, so we convert, tee, and
75-
// convert back again.
76-
// TODO: Switch to web streams without converting when #33442 has landed.
77-
const [readableWeb1, readableWeb2] = Readable.toWeb(readable).tee();
78-
cachedThirdPartyReadableWeb = readableWeb1;
79-
const result = createFromNodeStream(Readable.fromWeb(readableWeb2), {
80-
moduleMap: {},
81-
moduleLoading: {},
72+
serverModuleMap: null,
73+
moduleLoading: null,
74+
},
8275
});
83-
stream.pipe(readable);
84-
85-
return result;
8676
}
8777

8878
async function ServerComponent({noCache}) {

0 commit comments

Comments
 (0)