Skip to content

Commit 1d16396

Browse files
authored
Allow returning a temporary reference inside an async function (facebook#33761)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary Fixes `await`-ing and returning temporary references in `async` functions. These two operations invoke `.then()` under the hood if it is available, which currently results in an "Cannot access then on the server. You cannot dot into a temporary client reference..." error. This can easily be reproduced by returning a temporary reference from a server function. Fixes facebook#33534 ## How did you test this change? I added a test in a new test file. I wasn't sure where else to put it. <img width="771" height="138" alt="image" src="https://github.com/user-attachments/assets/09ffe6eb-271a-4842-a9fe-c68e17b3fb41" /> <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. -->
1 parent ddf8bc3 commit 1d16396

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/react-server/src/ReactFlightServerTemporaryReferences.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ const proxyHandlers = {
7070
`Instead, you can export a Client Component wrapper ` +
7171
`that itself renders a Client Context Provider.`,
7272
);
73+
// Allow returning a temporary reference from an async function
74+
case 'then':
75+
return undefined;
7376
}
7477
throw new Error(
7578
// eslint-disable-next-line react-internal/safe-string-coercion
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
* @emails react-core
8+
* @jest-environment node
9+
*/
10+
11+
'use strict';
12+
13+
let ReactFlightServerTemporaryReferences;
14+
15+
describe('ReactFlightServerTemporaryReferences', () => {
16+
beforeEach(() => {
17+
jest.resetModules();
18+
ReactFlightServerTemporaryReferences = require('react-server/src/ReactFlightServerTemporaryReferences');
19+
});
20+
21+
it('can return a temporary reference from an async function', async () => {
22+
const temporaryReferenceSet =
23+
ReactFlightServerTemporaryReferences.createTemporaryReferenceSet();
24+
const temporaryReference =
25+
ReactFlightServerTemporaryReferences.createTemporaryReference(
26+
temporaryReferenceSet,
27+
'test',
28+
);
29+
30+
async function foo() {
31+
return temporaryReference;
32+
}
33+
34+
await expect(foo()).resolves.toBe(temporaryReference);
35+
});
36+
});

0 commit comments

Comments
 (0)