Skip to content

Commit 60144a0

Browse files
authored
Split out Edge and Node implementations of the Flight Client (facebook#26187)
This splits out the Edge and Node implementations of Flight Client into their own implementations. The Node implementation now takes a Node Stream as input. I removed the bundler config from the Browser variant because you're never supposed to use that in the browser since it's only for SSR. Similarly, it's required on the server. This also enables generating a SSR manifest from the Webpack plugin. This is necessary for SSR so that you can reverse look up what a client module is called on the server. I also removed the option to pass a callServer from the server. We might want to add it back in the future but basically, we don't recommend calling Server Functions from render for initial render because if that happened client-side it would be a client-side waterfall. If it's never called in initial render, then it also shouldn't ever happen during SSR. This might be considered too restrictive. ~This also compiles the unbundled packages as ESM. This isn't strictly necessary because we only need access to dynamic import to load the modules but we don't have any other build options that leave `import(...)` intact, and seems appropriate that this would also be an ESM module.~ Went with `import(...)` in CJS instead.
1 parent 70b0bbd commit 60144a0

28 files changed

+657
-124
lines changed

fixtures/flight/loader/index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ async function babelLoad(url, context, defaultLoad) {
2323
const result = await defaultLoad(url, context, defaultLoad);
2424
if (result.format === 'module') {
2525
const opt = Object.assign({filename: url}, babelOptions);
26-
const {code} = await babel.transformAsync(result.source, opt);
27-
return {source: code, format: 'module'};
26+
const newResult = await babel.transformAsync(result.source, opt);
27+
if (!newResult) {
28+
if (typeof result.source === 'string') {
29+
return result;
30+
}
31+
return {
32+
source: Buffer.from(result.source).toString('utf8'),
33+
format: 'module',
34+
};
35+
}
36+
return {source: newResult.code, format: 'module'};
2837
}
2938
return defaultLoad(url, context, defaultLoad);
3039
}
@@ -39,8 +48,16 @@ async function babelTransformSource(source, context, defaultTransformSource) {
3948
const {format} = context;
4049
if (format === 'module') {
4150
const opt = Object.assign({filename: context.url}, babelOptions);
42-
const {code} = await babel.transformAsync(source, opt);
43-
return {source: code};
51+
const newResult = await babel.transformAsync(source, opt);
52+
if (!newResult) {
53+
if (typeof source === 'string') {
54+
return {source};
55+
}
56+
return {
57+
source: Buffer.from(source).toString('utf8'),
58+
};
59+
}
60+
return {source: newResult.code};
4461
}
4562
return defaultTransformSource(source, context, defaultTransformSource);
4663
}

fixtures/flight/server/handler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

3-
const {renderToPipeableStream} = require('react-server-dom-webpack/server');
43
const {readFile} = require('fs').promises;
54
const {resolve} = require('path');
65
const React = require('react');
76

87
module.exports = async function (req, res) {
8+
const {renderToPipeableStream} = await import(
9+
'react-server-dom-webpack/server'
10+
);
911
switch (req.method) {
1012
case 'POST': {
1113
const serverReference = JSON.parse(req.get('rsc-action'));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import {TextDecoder} from 'util';
11+
12+
export type StringDecoder = TextDecoder;
13+
14+
export const supportsBinaryStreams = true;
15+
16+
export function createStringDecoder(): StringDecoder {
17+
return new TextDecoder();
18+
}
19+
20+
const decoderOptions = {stream: true};
21+
22+
export function readPartialStringChunk(
23+
decoder: StringDecoder,
24+
buffer: Uint8Array,
25+
): string {
26+
return decoder.decode(buffer, decoderOptions);
27+
}
28+
29+
export function readFinalStringChunk(
30+
decoder: StringDecoder,
31+
buffer: Uint8Array,
32+
): string {
33+
return decoder.decode(buffer);
34+
}

packages/react-client/src/forks/ReactFlightClientHostConfig.dom-node-webpack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
* @flow
88
*/
99

10-
export * from 'react-client/src/ReactFlightClientHostConfigBrowser';
10+
export * from 'react-client/src/ReactFlightClientHostConfigNode';
1111
export * from 'react-client/src/ReactFlightClientHostConfigStream';
1212
export * from 'react-server-dom-webpack/src/ReactFlightClientWebpackBundlerConfig';

packages/react-client/src/forks/ReactFlightClientHostConfig.dom-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
* @flow
88
*/
99

10-
export * from 'react-client/src/ReactFlightClientHostConfigBrowser';
10+
export * from 'react-client/src/ReactFlightClientHostConfigNode';
1111
export * from 'react-client/src/ReactFlightClientHostConfigStream';
12-
export * from 'react-server-dom-webpack/src/ReactFlightClientWebpackBundlerConfig';
12+
export * from 'react-server-dom-webpack/src/ReactFlightClientNodeBundlerConfig';

packages/react-server-dom-webpack/client.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* @flow
88
*/
99

10-
export * from './src/ReactFlightDOMClient';
10+
export * from './src/ReactFlightDOMClientBrowser';

packages/react-server-dom-webpack/client.edge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* @flow
88
*/
99

10-
export * from './src/ReactFlightDOMClient';
10+
export * from './src/ReactFlightDOMClientEdge';

packages/react-server-dom-webpack/client.node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* @flow
88
*/
99

10-
export * from './src/ReactFlightDOMClient';
10+
export * from './src/ReactFlightDOMClientNode';

packages/react-server-dom-webpack/client.node.unbundled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* @flow
88
*/
99

10-
export * from './src/ReactFlightDOMClient';
10+
export * from './src/ReactFlightDOMClientNode';

0 commit comments

Comments
 (0)