-
Notifications
You must be signed in to change notification settings - Fork 48.7k
[RFC] useServerContextsForRefetch #23382
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
Conversation
fe700d1
to
5aa5554
Compare
Comparing: be229c5...6e9f716 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
|
<ServerContextContextProvider value={context}> | ||
<ServerContext.Provider value={value}>{children}</ServerContext.Provider> | ||
</ServerContextContextProvider> | ||
return React.createElement( |
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.
Prefer React.jsx
since that's now what JSX compiles to. Really people shouldn't do that themselves since we can change the compiler but we are us.
|
||
const {useContext, useMemo, createContext} = React; | ||
|
||
const ServerContextContext = createContext(null); |
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.
Thought provoking, but could this itself be a ServerContext? 🤯
If we wanted to enable this same hook to work from inside Server Components on the server so that you can serialize the context for later use by other server components (like pre-rendering other subtrees).
Now that I see it, I'm not sure about the name. It's partly for refetches but it's also for loading alternative branches of a subtree. That's kind of a refetch but also not quite. Let the bikeshedding commence. |
adfcd87
to
6228562
Compare
6228562
to
73065cc
Compare
@@ -0,0 +1,10 @@ | |||
/** |
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.
You'll need to add this to be built as a bundle too.
We'll also need a react-server-dom-relay
one for FB.
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.
You can also make tests out of these two in ReactFlightDOM-test and ReactFlightDOMRelay-test to ensure that you did. E.g. by porting the test you have above.
packages/react-client/package.json
Outdated
"exports": { | ||
".": "./flight.js", | ||
"./flight": "./flight.js", | ||
"./flight-hooks": "./flight-hooks.js" |
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 package is really just exposed as a way to build custom renderers but we don't even publish it.
You don't have to include an exports but if you do you need to include ./src/*
to export the source to the compiler. https://github.com/facebook/react/blob/main/packages%2Freact%2Fpackage.json#L30
It gets stripped out on publish.
if ((tuple[1]: any).$$typeof === REACT_PROVIDER_TYPE) { | ||
return createElement(ServerContextWrapper, null, { | ||
ServerContext: ((tuple[1]: any): ReactProviderType<any>)._context, | ||
...tuple[3], |
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.
You know it's going to be children and value, so you can just enumerate those here. It's faster than spread.
Hi @salazarm! Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention. You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
d581807
to
017e9f4
Compare
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
c3cbcb6
to
24b3257
Compare
24b3257
to
fe1d427
Compare
This is so we know which host config we're using for these. After a couple of future fixes I don't think I'll need this anymore but in the current approach it's needed.
This is not published to npm. It's meant for use in FB only.
So we import the deep import from these entry points instead. Build react-client/flight-hooks For use by implementors of renderers.
We don't support that syntax in the build toolchain.
This surfaces a suspected bug since we build flight-hooks separately it creates a separate copy of the client contexts.
fe1d427
to
6e9f716
Compare
Summary
Summary
This PR is stacked on top of #23244 (curse you github for not supporting stacked PRs!) If you want to see just this PR check out this commit: 5aa5554
This PR adds a client side hook to be used in conjunction with Flight/Fizz for using ServerContext.
useServerContextsForRefetch
This hook is meant to be used with Flight (RSC).
It returns the parent server contexts of the react component it is called for.
It returns the original server values, that is if the server context is overridden on the client that value will be ignored.
It returns the server contexts as a reverse linked list in the format:
eg:
Server.js
On the client if we wanted to re-render HybridComponent2 then we'd call the callback returned by useServerContextsForRefetch for HybridComponent2 and it would return [['ServerContext', 'Bar']]. Then on the server we can render using that context.
Note: useServerContextsForRefetch hook only makes sense for Hybrid components and not client components because trying to refetch a client only component with Flight would just result in Flight deferring to the client to render the component. However if you pass the result from Flight to Fizz to SSR the result then that could make sense for devices with really slow CPU however it usually isn't the right tradeoff since the components are already loaded on the client.
Why Refetch at all?
The main benefit to refetching a subtree in Flight rather than just re-rendering on the client can be seen when we consider request waterfalls. A request waterfall on the client includes latency from server <--> client multiple times. Where as a request waterfall on the server is a lot faster since the requests are all happening within your network.
How did you test this change?
jest