@samitouri / QOS-React-1 / commits / 9444c51c7e

[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.

Sebastian Markbåge committed Feb 19, 2024 at 11:49 UTC 9444c51c7e44936c24e1f44db905cfd45c51cff3
4 files changed +67 -9
packages/react-server-dom-esm/src/ReactFlightESMReferences.js
+3 -3
@@ -70,8 +70,8 @@ export function registerServerReference<T: Function>(
70 ): ServerReference<T> {
71 return Object.defineProperties((reference: any), {
72 $$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},
76 });
77 }
packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js
+6 -3
@@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
83 ): ServerReference<T> {
84 return Object.defineProperties((reference: any), {
85 $$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},
92 });
93 }
94
packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js
+6 -3
@@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
83 ): ServerReference<T> {
84 return Object.defineProperties((reference: any), {
85 $$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},
92 });
93 }
94
packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js
+52
@@ -1078,6 +1078,58 @@ describe('ReactFlightDOMBrowser', () => {
1078 }
1079 });
1080
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 +
1133 it('supports Float hints before the first await in server components in Fiber', async () => {
1134 function Component() {
1135 return <p>hello world</p>;