-
Notifications
You must be signed in to change notification settings - Fork 47k
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] Support Keyed Server Components #28123
Conversation
This keeps track of the keys from parent Server Components that are inherited down.
This is then prepended to the key of the terminal client element.
77db198
to
3a90397
Compare
If an element gets wrapped in a different server component then that has a different keyPath context and the element might end up with a different key. So we don't use the deduping mechanism if we're already inside a Server Component parent with a key or otherwise. Only the simple case gets deduped. The props of a client element are still deduped though if they're the same instance.
Conceptually a Server Component in the tree is the same as a Client Component. When we render a Server Component with a key, that key should be used as part of the reconciliation process to ensure the children's state are preserved when they move in a set. The key of a child should also be used to clear the state of the children when that key changes. Conversely, if a Server Component doesn't have a key it should get an implicit key based on the slot number. It should not inherit the key of its children since the children don't know if that would collide with other keys in the set the Server Component is rendered in. A Client Component also has an identity based on the function's implementation type. That mainly has to do with the state (or future state after a refactor) that Component might contain. To transfer state between two implementations it needs to be of the same state type. This is not a concern for a Server Components since they never have state so identity doesn't matter. A Component returns a set of children. If it returns a single child, that's the same as returning a fragment of one child. So if you conditionally return a single child or a fragment, they should technically reconcile against each other. The simple way to do this is to simply emit a Fragment for every Server Component. That would be correct in all cases. Unfortunately that is also unfortunate since it bloats the payload in the common cases. It also means that Fiber creates an extra indirection in the runtime. Ideally we want to fold Server Component aways into zero cost on the client. At least where possible. The common cases are that you don't specify a key on a single return child, and that you do specify a key on a Server Component in a dynamic set. The approach in this PR treats a Server Component that returns other Server Components or Lazy Nodes as a sequence that can be folded away. I.e. the parts that don't generate any output in the RSC payload. Instead, it keeps track of their keys on an internal "context". Which gets reset after each new reified JSON node gets rendered. Then we transfer the accumulated keys from any parent Server Components onto the child element. In the simple case, the child just inherits the key of the parent. If the Server Component itself is keyless but a child isn't, we have to add a wrapper fragment to ensure that this fragment gets the implicit key but we can still use the key to reset state. This is unusual though because typically if you keyed something it's because it was already in a fragment. In the case a Server Component is keyed but forks its children using a fragment, we need to key that fragment so that the whole set can move around as one. In theory this could be flattened into a parent array but that gets tricky if something suspends, because then we can't send the siblings early. The main downside of this approach is that switching between single child and fragment in a Server Component isn't always going to reconcile against each other. That's because if we saw a single child first, we'd have to add the fragment preemptively in case it forks later. This semantic of React isn't very well known anyway and it might be ok to break it here for pragmatic reasons. The tests document this discrepancy. Another compromise of this approach is that when combining keys we don't escape them fully. We instead just use a simple `,` separated concat. This is probably good enough in practice. Additionally, since we don't encode the implicit 0 index slot key, you can move things around between parents which shouldn't really reconcile but does. This keeps the keys shorter and more human readable. DiffTrain build for [95ec128](95ec128)
- facebook/react#28250 - facebook/react#28225 - facebook/react#28123 - facebook/react#28240 - facebook/react#28239 - facebook/react#28245 - facebook/react#28244 - facebook/react#28238 - facebook/react#28235 - facebook/react#28221 - facebook/react#28215 - facebook/react#28214 - facebook/react#28213 - facebook/react#28212 - facebook/react#28211 - facebook/react#28247 - facebook/react#28210 - facebook/react#28186 - facebook/react#28232 - facebook/react#28169 - facebook/react#28177 - facebook/react#28170 - facebook/react#28168 - facebook/react#28122 - facebook/react#27982 - facebook/react#28217 - facebook/react#28223 - facebook/react#28208 - facebook/react#28209 - facebook/react#28200 - facebook/react#28199 - facebook/react#28198 - facebook/react#28197 - facebook/react#28196 - facebook/react#28194 - facebook/react#28192 - facebook/react#28191 - facebook/react#28182 - facebook/react#28181 - facebook/react#28180 - facebook/react#28178 - facebook/react#28201 - facebook/react#28176 - facebook/react#28162 - facebook/react#28131 - facebook/react#28190 - facebook/react#28172 - facebook/react#28171 - facebook/react#28173 - facebook/react#28174 - facebook/react#28175 - facebook/react#28136 - facebook/react#28135 - facebook/react#28134 - facebook/react#28133 - facebook/react#28132 - facebook/react#28130 - facebook/react#28202 - facebook/react#28102 - facebook/react#28161 - facebook/react#28193 - facebook/react#28195 - facebook/react#28189 - facebook/react#28160 - facebook/react#28096 - facebook/react#28183 - facebook/react#28125 - facebook/react#28157 - facebook/react#28115 - facebook/react#28124 - facebook/react#28163 - facebook/react#28164 - facebook/react#28150 - facebook/react#28159 - facebook/react#28069 - facebook/react#28110 - facebook/react#28148 - facebook/react#28116 - facebook/react#28099 - facebook/react#28100 - facebook/react#28147 - facebook/react#28128 - facebook/react#28126 - facebook/react#28139 - facebook/react#28140 - facebook/react#28141 - facebook/react#28142 - facebook/react#28113 - facebook/react#28129 - facebook/react#28114 - facebook/react#28053 - facebook/react#28091 - facebook/react#28087 - facebook/react#28112 - facebook/react#28086 - facebook/react#28101 - facebook/react#28106 - facebook/react#28117 - facebook/react#28118 - facebook/react#28105 - facebook/react#27883 - facebook/react#28111 - facebook/react#28095 - facebook/react#28108 - facebook/react#28090 - facebook/react#28089 - facebook/react#28076 - facebook/react#28074 - facebook/react#28103 - facebook/react#28098 - facebook/react#28097 - facebook/react#28068 - facebook/react#28093 - facebook/react#28094 - facebook/react#28073 - facebook/react#28084 - facebook/react#28063 - facebook/react#28085 - facebook/react#28083 - facebook/react#28065 - facebook/react#28061 - facebook/react#28077 - facebook/react#28075 - facebook/react#28078 - facebook/react#28050 - facebook/react#28011 - facebook/react#28055 - facebook/react#28066 - facebook/react#28067 - facebook/react#28010 - facebook/react#27993 - facebook/react#28052 - facebook/react#28060 - facebook/react#28059 - facebook/react#28034 - facebook/react#28033 - facebook/react#28004 - facebook/react#28051 - facebook/react#28012 - facebook/react#28001 - facebook/react#28002 - facebook/react#27995 - facebook/react#28006 - facebook/react#28005 - facebook/react#28007 - facebook/react#28008 - facebook/react#28009 - facebook/react#28000 - facebook/react#28003 - facebook/react#27997 - facebook/react#27240 - facebook/react#27977 - facebook/react#27940 - facebook/react#27939 - facebook/react#28090 - facebook/react#28089 - facebook/react#28076 - facebook/react#28074 - facebook/react#28103 - facebook/react#28098 - facebook/react#28097 - facebook/react#28068 - facebook/react#28093 - facebook/react#28094 - facebook/react#28073 - facebook/react#28084 - facebook/react#28063 - facebook/react#28085 - facebook/react#28083 - facebook/react#28065 - facebook/react#28061 - facebook/react#28077 - facebook/react#28075 - facebook/react#28078 - facebook/react#28050 - facebook/react#28011 - facebook/react#28055 - facebook/react#28066 - facebook/react#28067 - facebook/react#28010 - facebook/react#27993 - facebook/react#28052 - facebook/react#28060 - facebook/react#28059 - facebook/react#28034 - facebook/react#28033 - facebook/react#28004 - facebook/react#28051 - facebook/react#28012 - facebook/react#28001 - facebook/react#28002 - facebook/react#27995 - facebook/react#28006 - facebook/react#28005 - facebook/react#28007 - facebook/react#28008 - facebook/react#28009 - facebook/react#28000 - facebook/react#28003 - facebook/react#27997 - facebook/react#27240 - facebook/react#27977 - facebook/react#27940 - facebook/react#27939
- facebook/react#28250 - facebook/react#28225 - facebook/react#28123 - facebook/react#28240 - facebook/react#28239 - facebook/react#28245 - facebook/react#28244 - facebook/react#28238 - facebook/react#28235 - facebook/react#28221 - facebook/react#28215 - facebook/react#28214 - facebook/react#28213 - facebook/react#28212 - facebook/react#28211 - facebook/react#28247 - facebook/react#28210 - facebook/react#28186 - facebook/react#28232 - facebook/react#28169 - facebook/react#28177 - facebook/react#28170 - facebook/react#28168 - facebook/react#28122 - facebook/react#27982 - facebook/react#28217 - facebook/react#28223 - facebook/react#28208 - facebook/react#28209 - facebook/react#28200 - facebook/react#28199 - facebook/react#28198 - facebook/react#28197 - facebook/react#28196 - facebook/react#28194 - facebook/react#28192 - facebook/react#28191 - facebook/react#28182 - facebook/react#28181 - facebook/react#28180 - facebook/react#28178 - facebook/react#28201 - facebook/react#28176 - facebook/react#28162 - facebook/react#28131 - facebook/react#28190 - facebook/react#28172 - facebook/react#28171 - facebook/react#28173 - facebook/react#28174 - facebook/react#28175 - facebook/react#28136 - facebook/react#28135 - facebook/react#28134 - facebook/react#28133 - facebook/react#28132 - facebook/react#28130 - facebook/react#28202 - facebook/react#28102 - facebook/react#28161 - facebook/react#28193 - facebook/react#28195 - facebook/react#28189 - facebook/react#28160 - facebook/react#28096 - facebook/react#28183 - facebook/react#28125 - facebook/react#28157 - facebook/react#28115 - facebook/react#28124 - facebook/react#28163 - facebook/react#28164 - facebook/react#28150 - facebook/react#28159 - facebook/react#28069 - facebook/react#28110 - facebook/react#28148 - facebook/react#28116 - facebook/react#28099 - facebook/react#28100 - facebook/react#28147 - facebook/react#28128 - facebook/react#28126 - facebook/react#28139 - facebook/react#28140 - facebook/react#28141 - facebook/react#28142 - facebook/react#28113 - facebook/react#28129 - facebook/react#28114 - facebook/react#28053 - facebook/react#28091 - facebook/react#28087 - facebook/react#28112 - facebook/react#28086 - facebook/react#28101 - facebook/react#28106 - facebook/react#28117 - facebook/react#28118 - facebook/react#28105 - facebook/react#27883 - facebook/react#28111 - facebook/react#28095 - facebook/react#28108 - facebook/react#28090 - facebook/react#28089 - facebook/react#28076 - facebook/react#28074 - facebook/react#28103 - facebook/react#28098 - facebook/react#28097 - facebook/react#28068 - facebook/react#28093 - facebook/react#28094 - facebook/react#28073 - facebook/react#28084 - facebook/react#28063 - facebook/react#28085 - facebook/react#28083 - facebook/react#28065 - facebook/react#28061 - facebook/react#28077 - facebook/react#28075 - facebook/react#28078 - facebook/react#28050 - facebook/react#28011 - facebook/react#28055 - facebook/react#28066 - facebook/react#28067 - facebook/react#28010 - facebook/react#27993 - facebook/react#28052 - facebook/react#28060 - facebook/react#28059 - facebook/react#28034 - facebook/react#28033 - facebook/react#28004 - facebook/react#28051 - facebook/react#28012 - facebook/react#28001 - facebook/react#28002 - facebook/react#27995 - facebook/react#28006 - facebook/react#28005 - facebook/react#28007 - facebook/react#28008 - facebook/react#28009 - facebook/react#28000 - facebook/react#28003 - facebook/react#27997 - facebook/react#27240 - facebook/react#27977 - facebook/react#27940 - facebook/react#27939 - facebook/react#28090 - facebook/react#28089 - facebook/react#28076 - facebook/react#28074 - facebook/react#28103 - facebook/react#28098 - facebook/react#28097 - facebook/react#28068 - facebook/react#28093 - facebook/react#28094 - facebook/react#28073 - facebook/react#28084 - facebook/react#28063 - facebook/react#28085 - facebook/react#28083 - facebook/react#28065 - facebook/react#28061 - facebook/react#28077 - facebook/react#28075 - facebook/react#28078 - facebook/react#28050 - facebook/react#28011 - facebook/react#28055 - facebook/react#28066 - facebook/react#28067 - facebook/react#28010 - facebook/react#27993 - facebook/react#28052 - facebook/react#28060 - facebook/react#28059 - facebook/react#28034 - facebook/react#28033 - facebook/react#28004 - facebook/react#28051 - facebook/react#28012 - facebook/react#28001 - facebook/react#28002 - facebook/react#27995 - facebook/react#28006 - facebook/react#28005 - facebook/react#28007 - facebook/react#28008 - facebook/react#28009 - facebook/react#28000 - facebook/react#28003 - facebook/react#27997 - facebook/react#27240 - facebook/react#27977 - facebook/react#27940 - facebook/react#27939
Updates React from 60a927d04 to 2bc7d336a Also updates aliases for `react.shared-subset` to `react.react-server` ### React upstream changes - facebook/react#28250 - facebook/react#28225 - facebook/react#28123 - facebook/react#28240 - facebook/react#28239 - facebook/react#28245 - facebook/react#28244 - facebook/react#28238 - facebook/react#28235 - facebook/react#28221 - facebook/react#28215 - facebook/react#28214 - facebook/react#28213 - facebook/react#28212 - facebook/react#28211 - facebook/react#28247 - facebook/react#28210 - facebook/react#28186 - facebook/react#28232 - facebook/react#28169 - facebook/react#28177 - facebook/react#28170 - facebook/react#28168 - facebook/react#28122 - facebook/react#27982 - facebook/react#28217 - facebook/react#28223 - facebook/react#28208 - facebook/react#28209 - facebook/react#28200 - facebook/react#28199 - facebook/react#28198 - facebook/react#28197 - facebook/react#28196 - facebook/react#28194 - facebook/react#28192 - facebook/react#28191 - facebook/react#28182 - facebook/react#28181 - facebook/react#28180 - facebook/react#28178 - facebook/react#28201 - facebook/react#28176 - facebook/react#28162 - facebook/react#28131 - facebook/react#28190 - facebook/react#28172 - facebook/react#28171 - facebook/react#28173 - facebook/react#28174 - facebook/react#28175 - facebook/react#28136 - facebook/react#28135 - facebook/react#28134 - facebook/react#28133 - facebook/react#28132 - facebook/react#28130 - facebook/react#28202 - facebook/react#28102 - facebook/react#28161 - facebook/react#28193 - facebook/react#28195 - facebook/react#28189 - facebook/react#28160 - facebook/react#28096 - facebook/react#28183 - facebook/react#28125 - facebook/react#28157 - facebook/react#28115 - facebook/react#28124 - facebook/react#28163 - facebook/react#28164 - facebook/react#28150 - facebook/react#28159 - facebook/react#28069 - facebook/react#28110 - facebook/react#28148 - facebook/react#28116 - facebook/react#28099 - facebook/react#28100 - facebook/react#28147 - facebook/react#28128 - facebook/react#28126 - facebook/react#28139 - facebook/react#28140 - facebook/react#28141 - facebook/react#28142 - facebook/react#28113 - facebook/react#28129 - facebook/react#28114 - facebook/react#28053 - facebook/react#28091 - facebook/react#28087 - facebook/react#28112 - facebook/react#28086 - facebook/react#28101 - facebook/react#28106 - facebook/react#28117 - facebook/react#28118 - facebook/react#28105 - facebook/react#27883 - facebook/react#28111 - facebook/react#28095 - facebook/react#28108 - facebook/react#28090 - facebook/react#28089 - facebook/react#28076 - facebook/react#28074 - facebook/react#28103 - facebook/react#28098 - facebook/react#28097 - facebook/react#28068 - facebook/react#28093 - facebook/react#28094 - facebook/react#28073 - facebook/react#28084 - facebook/react#28063 - facebook/react#28085 - facebook/react#28083 - facebook/react#28065 - facebook/react#28061 - facebook/react#28077 - facebook/react#28075 - facebook/react#28078 - facebook/react#28050 - facebook/react#28011 - facebook/react#28055 - facebook/react#28066 - facebook/react#28067 - facebook/react#28010 - facebook/react#27993 - facebook/react#28052 - facebook/react#28060 - facebook/react#28059 - facebook/react#28034 - facebook/react#28033 - facebook/react#28004 - facebook/react#28051 - facebook/react#28012 - facebook/react#28001 - facebook/react#28002 - facebook/react#27995 - facebook/react#28006 - facebook/react#28005 - facebook/react#28007 - facebook/react#28008 - facebook/react#28009 - facebook/react#28000 - facebook/react#28003 - facebook/react#27997 - facebook/react#27240 - facebook/react#27977 - facebook/react#27940 - facebook/react#27939 - facebook/react#28090 - facebook/react#28089 - facebook/react#28076 - facebook/react#28074 - facebook/react#28103 - facebook/react#28098 - facebook/react#28097 - facebook/react#28068 - facebook/react#28093 - facebook/react#28094 - facebook/react#28073 - facebook/react#28084 - facebook/react#28063 - facebook/react#28085 - facebook/react#28083 - facebook/react#28065 - facebook/react#28061 - facebook/react#28077 - facebook/react#28075 - facebook/react#28078 - facebook/react#28050 - facebook/react#28011 - facebook/react#28055 - facebook/react#28066 - facebook/react#28067 - facebook/react#28010 - facebook/react#27993 - facebook/react#28052 - facebook/react#28060 - facebook/react#28059 - facebook/react#28034 - facebook/react#28033 - facebook/react#28004 - facebook/react#28051 - facebook/react#28012 - facebook/react#28001 - facebook/react#28002 - facebook/react#27995 - facebook/react#28006 - facebook/react#28005 - facebook/react#28007 - facebook/react#28008 - facebook/react#28009 - facebook/react#28000 - facebook/react#28003 - facebook/react#27997 - facebook/react#27240 - facebook/react#27977 - facebook/react#27940 - facebook/react#27939 Closes NEXT-2331
I believe I have encountered a use case that might benefit from this, so let me share it in case you find it valid or helpful. const ServerComponent = async (props) => {
return (
<React.Suspense fallback={<>{props.children}</>}>
<AsyncComponentThatWouldGetDataAndRenderClientProvider>
<>
{props.children}
</>
</AsyncComponentThatWouldGetDataAndRenderClientProvider>
</React.Suspense>
)
} This code seems to work, however, in the streamed payload the What I've been wondering is, if key-ing the server component ( |
No. Key doesn't have anything to do with that. RSC does dedupe after the third copy of an object has been detected. However, with this change it'll actually be able to apply that optimization in fewer cases than before this change. In theory the heuristic can get smarter and dedupe this but it has other tradeoffs on performance. The way to fix it though is to just turn that whole thing into a client component. |
I don't have time to finish all the tests but here are some test cases that should be tested:
|
…suspend (#28283) In #28123 I switched these to be lazy references. However that creates a lazy wrapper even if they're synchronously available. We try to as much as possible preserve the original data structure in these cases. E.g. here in the dev outlining I only use a lazy wrapper if it didn't complete synchronously: https://github.com/facebook/react/pull/28272/files#diff-d4c9c509922b3671d3ecce4e051df66dd5c3d38ff913c7a7fe94abc3ba2ed72eR638 Unfortunately we don't have a data structure that tracks the status of each emitted row. We could store the task in the map but then they couldn't be GC:ed as they complete. We could maybe store the status of each element but seems so heavy. For now I just went back to direct reference which might be an issue since it can suspend something higher up when deduped.
…suspend (#28283) In #28123 I switched these to be lazy references. However that creates a lazy wrapper even if they're synchronously available. We try to as much as possible preserve the original data structure in these cases. E.g. here in the dev outlining I only use a lazy wrapper if it didn't complete synchronously: https://github.com/facebook/react/pull/28272/files#diff-d4c9c509922b3671d3ecce4e051df66dd5c3d38ff913c7a7fe94abc3ba2ed72eR638 Unfortunately we don't have a data structure that tracks the status of each emitted row. We could store the task in the map but then they couldn't be GC:ed as they complete. We could maybe store the status of each element but seems so heavy. For now I just went back to direct reference which might be an issue since it can suspend something higher up when deduped. DiffTrain build for [ba5e6a8](ba5e6a8)
Conceptually a Server Component in the tree is the same as a Client Component. When we render a Server Component with a key, that key should be used as part of the reconciliation process to ensure the children's state are preserved when they move in a set. The key of a child should also be used to clear the state of the children when that key changes. Conversely, if a Server Component doesn't have a key it should get an implicit key based on the slot number. It should not inherit the key of its children since the children don't know if that would collide with other keys in the set the Server Component is rendered in. A Client Component also has an identity based on the function's implementation type. That mainly has to do with the state (or future state after a refactor) that Component might contain. To transfer state between two implementations it needs to be of the same state type. This is not a concern for a Server Components since they never have state so identity doesn't matter. A Component returns a set of children. If it returns a single child, that's the same as returning a fragment of one child. So if you conditionally return a single child or a fragment, they should technically reconcile against each other. The simple way to do this is to simply emit a Fragment for every Server Component. That would be correct in all cases. Unfortunately that is also unfortunate since it bloats the payload in the common cases. It also means that Fiber creates an extra indirection in the runtime. Ideally we want to fold Server Component aways into zero cost on the client. At least where possible. The common cases are that you don't specify a key on a single return child, and that you do specify a key on a Server Component in a dynamic set. The approach in this PR treats a Server Component that returns other Server Components or Lazy Nodes as a sequence that can be folded away. I.e. the parts that don't generate any output in the RSC payload. Instead, it keeps track of their keys on an internal "context". Which gets reset after each new reified JSON node gets rendered. Then we transfer the accumulated keys from any parent Server Components onto the child element. In the simple case, the child just inherits the key of the parent. If the Server Component itself is keyless but a child isn't, we have to add a wrapper fragment to ensure that this fragment gets the implicit key but we can still use the key to reset state. This is unusual though because typically if you keyed something it's because it was already in a fragment. In the case a Server Component is keyed but forks its children using a fragment, we need to key that fragment so that the whole set can move around as one. In theory this could be flattened into a parent array but that gets tricky if something suspends, because then we can't send the siblings early. The main downside of this approach is that switching between single child and fragment in a Server Component isn't always going to reconcile against each other. That's because if we saw a single child first, we'd have to add the fragment preemptively in case it forks later. This semantic of React isn't very well known anyway and it might be ok to break it here for pragmatic reasons. The tests document this discrepancy. Another compromise of this approach is that when combining keys we don't escape them fully. We instead just use a simple `,` separated concat. This is probably good enough in practice. Additionally, since we don't encode the implicit 0 index slot key, you can move things around between parents which shouldn't really reconcile but does. This keeps the keys shorter and more human readable.
…suspend (facebook#28283) In facebook#28123 I switched these to be lazy references. However that creates a lazy wrapper even if they're synchronously available. We try to as much as possible preserve the original data structure in these cases. E.g. here in the dev outlining I only use a lazy wrapper if it didn't complete synchronously: https://github.com/facebook/react/pull/28272/files#diff-d4c9c509922b3671d3ecce4e051df66dd5c3d38ff913c7a7fe94abc3ba2ed72eR638 Unfortunately we don't have a data structure that tracks the status of each emitted row. We could store the task in the map but then they couldn't be GC:ed as they complete. We could maybe store the status of each element but seems so heavy. For now I just went back to direct reference which might be an issue since it can suspend something higher up when deduped.
Conceptually a Server Component in the tree is the same as a Client Component. When we render a Server Component with a key, that key should be used as part of the reconciliation process to ensure the children's state are preserved when they move in a set. The key of a child should also be used to clear the state of the children when that key changes. Conversely, if a Server Component doesn't have a key it should get an implicit key based on the slot number. It should not inherit the key of its children since the children don't know if that would collide with other keys in the set the Server Component is rendered in. A Client Component also has an identity based on the function's implementation type. That mainly has to do with the state (or future state after a refactor) that Component might contain. To transfer state between two implementations it needs to be of the same state type. This is not a concern for a Server Components since they never have state so identity doesn't matter. A Component returns a set of children. If it returns a single child, that's the same as returning a fragment of one child. So if you conditionally return a single child or a fragment, they should technically reconcile against each other. The simple way to do this is to simply emit a Fragment for every Server Component. That would be correct in all cases. Unfortunately that is also unfortunate since it bloats the payload in the common cases. It also means that Fiber creates an extra indirection in the runtime. Ideally we want to fold Server Component aways into zero cost on the client. At least where possible. The common cases are that you don't specify a key on a single return child, and that you do specify a key on a Server Component in a dynamic set. The approach in this PR treats a Server Component that returns other Server Components or Lazy Nodes as a sequence that can be folded away. I.e. the parts that don't generate any output in the RSC payload. Instead, it keeps track of their keys on an internal "context". Which gets reset after each new reified JSON node gets rendered. Then we transfer the accumulated keys from any parent Server Components onto the child element. In the simple case, the child just inherits the key of the parent. If the Server Component itself is keyless but a child isn't, we have to add a wrapper fragment to ensure that this fragment gets the implicit key but we can still use the key to reset state. This is unusual though because typically if you keyed something it's because it was already in a fragment. In the case a Server Component is keyed but forks its children using a fragment, we need to key that fragment so that the whole set can move around as one. In theory this could be flattened into a parent array but that gets tricky if something suspends, because then we can't send the siblings early. The main downside of this approach is that switching between single child and fragment in a Server Component isn't always going to reconcile against each other. That's because if we saw a single child first, we'd have to add the fragment preemptively in case it forks later. This semantic of React isn't very well known anyway and it might be ok to break it here for pragmatic reasons. The tests document this discrepancy. Another compromise of this approach is that when combining keys we don't escape them fully. We instead just use a simple `,` separated concat. This is probably good enough in practice. Additionally, since we don't encode the implicit 0 index slot key, you can move things around between parents which shouldn't really reconcile but does. This keeps the keys shorter and more human readable. DiffTrain build for commit 95ec128.
…suspend (#28283) In #28123 I switched these to be lazy references. However that creates a lazy wrapper even if they're synchronously available. We try to as much as possible preserve the original data structure in these cases. E.g. here in the dev outlining I only use a lazy wrapper if it didn't complete synchronously: https://github.com/facebook/react/pull/28272/files#diff-d4c9c509922b3671d3ecce4e051df66dd5c3d38ff913c7a7fe94abc3ba2ed72eR638 Unfortunately we don't have a data structure that tracks the status of each emitted row. We could store the task in the map but then they couldn't be GC:ed as they complete. We could maybe store the status of each element but seems so heavy. For now I just went back to direct reference which might be an issue since it can suspend something higher up when deduped. DiffTrain build for commit ba5e6a8.
Conceptually a Server Component in the tree is the same as a Client Component.
When we render a Server Component with a key, that key should be used as part of the reconciliation process to ensure the children's state are preserved when they move in a set. The key of a child should also be used to clear the state of the children when that key changes.
Conversely, if a Server Component doesn't have a key it should get an implicit key based on the slot number. It should not inherit the key of its children since the children don't know if that would collide with other keys in the set the Server Component is rendered in.
A Client Component also has an identity based on the function's implementation type. That mainly has to do with the state (or future state after a refactor) that Component might contain. To transfer state between two implementations it needs to be of the same state type. This is not a concern for a Server Components since they never have state so identity doesn't matter.
A Component returns a set of children. If it returns a single child, that's the same as returning a fragment of one child. So if you conditionally return a single child or a fragment, they should technically reconcile against each other.
The simple way to do this is to simply emit a Fragment for every Server Component. That would be correct in all cases. Unfortunately that is also unfortunate since it bloats the payload in the common cases. It also means that Fiber creates an extra indirection in the runtime.
Ideally we want to fold Server Component aways into zero cost on the client. At least where possible. The common cases are that you don't specify a key on a single return child, and that you do specify a key on a Server Component in a dynamic set.
The approach in this PR treats a Server Component that returns other Server Components or Lazy Nodes as a sequence that can be folded away. I.e. the parts that don't generate any output in the RSC payload. Instead, it keeps track of their keys on an internal "context". Which gets reset after each new reified JSON node gets rendered.
Then we transfer the accumulated keys from any parent Server Components onto the child element. In the simple case, the child just inherits the key of the parent.
If the Server Component itself is keyless but a child isn't, we have to add a wrapper fragment to ensure that this fragment gets the implicit key but we can still use the key to reset state. This is unusual though because typically if you keyed something it's because it was already in a fragment.
In the case a Server Component is keyed but forks its children using a fragment, we need to key that fragment so that the whole set can move around as one. In theory this could be flattened into a parent array but that gets tricky if something suspends, because then we can't send the siblings early.
The main downside of this approach is that switching between single child and fragment in a Server Component isn't always going to reconcile against each other. That's because if we saw a single child first, we'd have to add the fragment preemptively in case it forks later. This semantic of React isn't very well known anyway and it might be ok to break it here for pragmatic reasons. The tests document this discrepancy.
Another compromise of this approach is that when combining keys we don't escape them fully. We instead just use a simple
,
separated concat. This is probably good enough in practice. Additionally, since we don't encode the implicit 0 index slot key, you can move things around between parents which shouldn't really reconcile but does. This keeps the keys shorter and more human readable.