Skip to content

[Flight] Add a cached 3rd-party component to the Flight fixture #33443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions fixtures/flight/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as React from 'react';
import {renderToPipeableStream} from 'react-server-dom-webpack/server';
import {createFromNodeStream} from 'react-server-dom-webpack/client';
import {PassThrough, Readable} from 'stream';

import Container from './Container.js';

Expand Down Expand Up @@ -35,8 +38,50 @@ async function Bar({children}) {
return <div>{children}</div>;
}

async function ThirdPartyComponent() {
return new Promise(resolve =>
setTimeout(() => resolve('hello from a 3rd party'), 30)
);
}

// Using Web streams for tee'ing convenience here.
let cachedThirdPartyReadableWeb;

function fetchThirdParty(Component) {
if (cachedThirdPartyReadableWeb) {
const [readableWeb1, readableWeb2] = cachedThirdPartyReadableWeb.tee();
cachedThirdPartyReadableWeb = readableWeb1;

return createFromNodeStream(Readable.fromWeb(readableWeb2), {
moduleMap: {},
moduleLoading: {},
});
}

const stream = renderToPipeableStream(
<ThirdPartyComponent />,
{},
{environmentName: 'third-party'}
);

const readable = new PassThrough();
// React currently only supports piping to one stream, so we convert, tee, and
// convert back again.
// TODO: Switch to web streams without converting when #33442 has landed.
const [readableWeb1, readableWeb2] = Readable.toWeb(readable).tee();
cachedThirdPartyReadableWeb = readableWeb1;
const result = createFromNodeStream(Readable.fromWeb(readableWeb2), {
moduleMap: {},
moduleLoading: {},
});
stream.pipe(readable);

return result;
}

async function ServerComponent() {
await new Promise(resolve => setTimeout(() => resolve('deferred text'), 50));
return await fetchThirdParty();
}

export default async function App({prerender}) {
Expand Down
Loading