Skip to content

Commit 0065939

Browse files
committed
Allow a Server Reference to be registered twice
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 a7144f2 commit 0065939

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export function registerServerReference<T>(
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

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ export function registerServerReference<T>(
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

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ export function registerServerReference<T>(
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

+52
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)