Repository: facebook/react
Pull request: [Flight] 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.



Changed files:
filename: packages/react-server-dom-esm/src/ReactFlightESMReferences.js
patch: @@ -70,8 +70,8 @@ export function registerServerReference<T: Function>(
 ): ServerReference<T> {
   return Object.defineProperties((reference: any), {
     $$typeof: {value: SERVER_REFERENCE_TAG},
-    $$id: {value: id + '#' + exportName},
-    $$bound: {value: null},
-    bind: {value: bind},
+    $$id: {value: id + '#' + exportName, configurable: true},
+    $$bound: {value: null, configurable: true},
+    bind: {value: bind, configurable: true},
   });
 }

filename: packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js
patch: @@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
 ): ServerReference<T> {
   return Object.defineProperties((reference: any), {
     $$typeof: {value: SERVER_REFERENCE_TAG},
-    $$id: {value: exportName === null ? id : id + '#' + exportName},
-    $$bound: {value: null},
-    bind: {value: bind},
+    $$id: {
+      value: exportName === null ? id : id + '#' + exportName,
+      configurable: true,
+    },
+    $$bound: {value: null, configurable: true},
+    bind: {value: bind, configurable: true},
   });
 }
 

filename: packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js
patch: @@ -83,9 +83,12 @@ export function registerServerReference<T: Function>(
 ): ServerReference<T> {
   return Object.defineProperties((reference: any), {
     $$typeof: {value: SERVER_REFERENCE_TAG},
-    $$id: {value: exportName === null ? id : id + '#' + exportName},
-    $$bound: {value: null},
-    bind: {value: bind},
+    $$id: {
+      value: exportName === null ? id : id + '#' + exportName,
+      configurable: true,
+    },
+    $$bound: {value: null, configurable: true},
+    bind: {value: bind, configurable: true},
   });
 }
 

filename: packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js
patch: @@ -1078,6 +1078,58 @@ describe('ReactFlightDOMBrowser', () => {
     }
   });
 
+  it('can use the same function twice as a server action', async () => {
+    let actionProxy1;
+    let actionProxy2;
+
+    function Client({action1, action2}) {
+      actionProxy1 = action1;
+      actionProxy2 = action2;
+      return 'Click Me';
+    }
+
+    function greet(text) {
+      return 'Hello ' + text;
+    }
+
+    const ServerModule = serverExports({
+      greet,
+      greet2: greet,
+    });
+    const ClientRef = clientExports(Client);
+
+    const stream = ReactServerDOMServer.renderToReadableStream(
+      <ClientRef action1={ServerModule.greet} action2={ServerModule.greet2} />,
+      webpackMap,
+    );
+
+    const response = ReactServerDOMClient.createFromReadableStream(stream, {
+      async callServer(ref, args) {
+        const body = await ReactServerDOMClient.encodeReply(args);
+        return callServer(ref, body);
+      },
+    });
+
+    function App() {
+      return use(response);
+    }
+
+    const container = document.createElement('div');
+    const root = ReactDOMClient.createRoot(container);
+    await act(() => {
+      root.render(<App />);
+    });
+    expect(container.innerHTML).toBe('Click Me');
+    expect(typeof actionProxy1).toBe('function');
+    expect(actionProxy1).not.toBe(greet);
+
+    // TODO: Ideally flight would be encoding this the same.
+    expect(actionProxy1).not.toBe(actionProxy2);
+
+    const result = await actionProxy1('world');
+    expect(result).toBe('Hello world');
+  });
+
   it('supports Float hints before the first await in server components in Fiber', async () => {
     function Component() {
       return <p>hello world</p>;