-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[Flight] add support for Lazy components in Flight server #24068
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,10 +200,21 @@ function attemptResolveElement( | |
return [REACT_ELEMENT_TYPE, type, key, props]; | ||
} | ||
switch (type.$$typeof) { | ||
case REACT_LAZY_TYPE: { | ||
const payload = type._payload; | ||
const init = type._init; | ||
const wrappedType = init(payload); | ||
return attemptResolveElement(wrappedType, key, ref, props); | ||
} | ||
case REACT_FORWARD_REF_TYPE: { | ||
const render = type.render; | ||
return render(props, undefined); | ||
} | ||
case REACT_ELEMENT_TYPE: { | ||
// this can happen when a lazy component resolves to an element instead of | ||
// a Component. | ||
return attemptResolveElement(type.type, type.key, type.ref, type.props); | ||
} | ||
case REACT_MEMO_TYPE: { | ||
return attemptResolveElement(type.type, key, ref, props); | ||
} | ||
|
@@ -452,10 +463,6 @@ export function resolveModelToJSON( | |
switch (value) { | ||
case REACT_ELEMENT_TYPE: | ||
return '$'; | ||
case REACT_LAZY_TYPE: | ||
throw new Error( | ||
'React Lazy Components are not yet supported on the server.', | ||
); | ||
Comment on lines
-455
to
-458
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In testing I could not ever get this case to throw. the value at this point is a React.Element and not a symbol so it ends up throwing in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can now use lazy in the position of React "nodes" (i.e. children). That's actually what Flight itself does on the client. const element = React.lazy(async () => {
return <Component prop="prop" />;
});
...
<div>{element}</div> That should probably ideally work in Server Components too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also technically, you could pass just the symbol. Regardless, if we see it, it's probably a bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add support for it, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should we do if someone does this const ElementDisguisedAsAComponent = React.lazy(async () => {
return <Component prop={"original"} />;
});
...
<div><ElementDisguisedAsAComponent prop={"new"}</div> feels like that should error in Dev and in prod we should render the actual element throwing away the "new" prop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works the way I said it should in prod already and there is no conditional, we could add the conditional in dev only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens in the client when you do this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the opposite is supported const componentDisguisedAsElement = React.lazy(async () => {
return Component;
});
...
<div>{componentDisguisedAsElement}</div> it will render like you did i.e. empty props. I'm thinking this should also error in dev There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The componentDisguisedAsElement case seems like it should hit this case? Regardless, it should behave as if there was no lazy around it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's a good hueristic, I've updated tests and behavior to have these match. It'll be an error to use lazy elements as Components and vice versa just like without lazy |
||
} | ||
|
||
if (__DEV__) { | ||
|
@@ -477,23 +484,34 @@ export function resolveModelToJSON( | |
while ( | ||
typeof value === 'object' && | ||
value !== null && | ||
(value: any).$$typeof === REACT_ELEMENT_TYPE | ||
((value: any).$$typeof === REACT_ELEMENT_TYPE || | ||
(value: any).$$typeof === REACT_LAZY_TYPE) | ||
) { | ||
if (__DEV__) { | ||
if (isInsideContextValue) { | ||
console.error('React elements are not allowed in ServerContext'); | ||
} | ||
} | ||
// TODO: Concatenate keys of parents onto children. | ||
const element: React$Element<any> = (value: any); | ||
|
||
try { | ||
// Attempt to render the server component. | ||
value = attemptResolveElement( | ||
element.type, | ||
element.key, | ||
element.ref, | ||
element.props, | ||
); | ||
switch ((value: any).$$typeof) { | ||
case REACT_ELEMENT_TYPE: { | ||
// TODO: Concatenate keys of parents onto children. | ||
const element: React$Element<any> = (value: any); | ||
// Attempt to render the server component. | ||
value = attemptResolveElement( | ||
element.type, | ||
element.key, | ||
element.ref, | ||
element.props, | ||
); | ||
break; | ||
} | ||
case REACT_LAZY_TYPE: { | ||
value = attemptResolveElement(value, null, null, {}); | ||
break; | ||
} | ||
} | ||
} catch (x) { | ||
if (typeof x === 'object' && x !== null && typeof x.then === 'function') { | ||
// Something suspended, we'll need to create a new segment and resolve it later. | ||
|
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.
I think the solution is to just remove this and then let it fall through to the error case. You could also put custom error messages at the bottom of this case if you want to explain in plain words to the user what's wrong.
Semantically this is not right since it's dropping key, ref and props from the parent.
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.
yup, the need for this went away when I added lazy resolution to
resolveModelToJSON