New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow passing client exports through async code #26666
Conversation
Comparing: da6c23a...91b3cc6 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little tricky because Promise.resolve(...)
is correct since it yields another Promise but then awaiting it is not correct.
Imagine the client export is Promise<string>
, if you await it before passing it along it's supposed to unwrap it.
<ClientComponent text={await ClientModule.asyncText} />
Type checking this would pass because text
would accept string
and await asyncText
unwraps the promise before passing it. The client reference is supposed to be a reference that unwraps it on the client before passing it along which this wouldn't do. So it's a runtime error.
We only really special case unwrapping shallowly for the module object since it's used to polyfill ESM.
Well what I ran into is import { Client, somePojo } from './client.js';
async function getThing() {
if (cond) {
return await fetch(...);
} else {
return somePojo;
}
}
async function App() {
const thing = await getThing();
return <Client data={thing} />;
} which felt like it should work. I could make a second async flag on the client reference that awaits the named export. Does that sound sensible? |
Not really, we have to put a limit to how much we encode in this otherwise we might as well have infinite dotting into things but the further you go, the more confusing it gets and the more we have to encode. The async module thing is only applicable to CommonJS and not ESM. So it's already not a general purpose mechanism. The semantics are really that you can just pass an identifier from the import to the client, without touching it. Because this is syntactic, it doesn't feel like you are but because of the weird Promise resolution it does. Maybe we need a better error message - but I feel like the error is still appropriate. A possible workaround is wrapping it in an object like |
Sad. Thanks. |
No description provided.