Skip to content

Commit 9444c51

Browse files
authored
[Flight] Allow a Server Reference to be registered twice (#28343)
It's possible for the same function instance to appear more than once in the same graph or even the same file. Currently this errors on trying to reconfigure the property but it really doesn't matter which one wins. First or last. Regardless there will be an entry point generated that can get them.
1 parent c323f82 commit 9444c51

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

packages/react-server-dom-esm/src/ReactFlightESMReferences.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export function registerServerReference<T: Function>(
7070
): ServerReference<T> {
7171
return Object.defineProperties((reference: any), {
7272
$$typeof: {value: SERVER_REFERENCE_TAG},
73-
$$id: {value: id + '#' + exportName},
74-
$$bound: {value: null},
75-
bind: {value: bind},
73+
$$id: {value: id + '#' + exportName, configurable: true},
74+
$$bound: {value: null, configurable: true},
75+
bind: {value: bind, configurable: true},
7676
});
7777
}

packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
8383
): ServerReference<T> {
8484
return Object.defineProperties((reference: any), {
8585
$$typeof: {value: SERVER_REFERENCE_TAG},
86-
$$id: {value: exportName === null ? id : id + '#' + exportName},
87-
$$bound: {value: null},
88-
bind: {value: bind},
86+
$$id: {
87+
value: exportName === null ? id : id + '#' + exportName,
88+
configurable: true,
89+
},
90+
$$bound: {value: null, configurable: true},
91+
bind: {value: bind, configurable: true},
8992
});
9093
}
9194

packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
8383
): ServerReference<T> {
8484
return Object.defineProperties((reference: any), {
8585
$$typeof: {value: SERVER_REFERENCE_TAG},
86-
$$id: {value: exportName === null ? id : id + '#' + exportName},
87-
$$bound: {value: null},
88-
bind: {value: bind},
86+
$$id: {
87+
value: exportName === null ? id : id + '#' + exportName,
88+
configurable: true,
89+
},
90+
$$bound: {value: null, configurable: true},
91+
bind: {value: bind, configurable: true},
8992
});
9093
}
9194

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,58 @@ describe('ReactFlightDOMBrowser', () => {
10781078
}
10791079
});
10801080

1081+
it('can use the same function twice as a server action', async () => {
1082+
let actionProxy1;
1083+
let actionProxy2;
1084+
1085+
function Client({action1, action2}) {
1086+
actionProxy1 = action1;
1087+
actionProxy2 = action2;
1088+
return 'Click Me';
1089+
}
1090+
1091+
function greet(text) {
1092+
return 'Hello ' + text;
1093+
}
1094+
1095+
const ServerModule = serverExports({
1096+
greet,
1097+
greet2: greet,
1098+
});
1099+
const ClientRef = clientExports(Client);
1100+
1101+
const stream = ReactServerDOMServer.renderToReadableStream(
1102+
<ClientRef action1={ServerModule.greet} action2={ServerModule.greet2} />,
1103+
webpackMap,
1104+
);
1105+
1106+
const response = ReactServerDOMClient.createFromReadableStream(stream, {
1107+
async callServer(ref, args) {
1108+
const body = await ReactServerDOMClient.encodeReply(args);
1109+
return callServer(ref, body);
1110+
},
1111+
});
1112+
1113+
function App() {
1114+
return use(response);
1115+
}
1116+
1117+
const container = document.createElement('div');
1118+
const root = ReactDOMClient.createRoot(container);
1119+
await act(() => {
1120+
root.render(<App />);
1121+
});
1122+
expect(container.innerHTML).toBe('Click Me');
1123+
expect(typeof actionProxy1).toBe('function');
1124+
expect(actionProxy1).not.toBe(greet);
1125+
1126+
// TODO: Ideally flight would be encoding this the same.
1127+
expect(actionProxy1).not.toBe(actionProxy2);
1128+
1129+
const result = await actionProxy1('world');
1130+
expect(result).toBe('Hello world');
1131+
});
1132+
10811133
it('supports Float hints before the first await in server components in Fiber', async () => {
10821134
function Component() {
10831135
return <p>hello world</p>;

0 commit comments

Comments
 (0)