-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Support async render function in getDataFromTree / getMarkupFromTree #6576
Support async render function in getDataFromTree / getMarkupFromTree #6576
Conversation
@richardscarrott: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/ |
80be842
to
666fbf3
Compare
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 like this idea and I don't see any problems with it, since this code is already async. However, I think we can avoid using async
/await
syntax here, in favor of the vanilla Promise
API, so I pushed a couple of commits to do that. No action needed on your part, unless you have additional thoughts. Thanks!
Heads up: we're not quite ready to publish |
Hey @benjamn thanks for merging this, much appreciated! Does |
Somewhat relatedly, I quickly setup a test repo for this change and noticed, even when going through the normal sync approach, calling |
@richardscarrott You can now In fact, if you keep using |
This is exactly the feature I needed and seems to work with renderToNodeStream. However i am seeing another issue in
This does not happen in |
@JamesCharlesworth See 4589606 for a quick fix for that problem. Thanks for letting us know about it. |
This enables importing the @apollo/client/testing entry point in environments that don't define global.it. Another take on solving #6576 (comment), given that 4589606 had to be reverted.
A safer way to solve #6576 (comment), given that 4589606 had to be reverted. This change makes it safe to import the `@apollo/client/testing` entry point in environments that don't define `global.it`.
Following up: we just published |
Great, thanks for the quick response @benjamn! |
@benjamn Just tested with the correct import and it works great, thanks! As an aside, I just noticed Apollo exposes Also, I guess there must have once been a significant performance difference between |
This is not entirely correct for anyone else that comes across this. Just FYI cc @JamesCharlesworth https://github.com/apollographql/apollo-client/blob/master/src/react/ssr/getDataFromTree.ts#L52 this throws away HTML and then repeats the process if if finds promises from rendering. Therefor if you attempt to pass in render to node stream with:
It will "work" but HTML will have been already flushed, meaning Apollo would have rendered in the This won't work until SSR suspense supported: https://reactjs.org/docs/concurrent-mode-suspense.html (mentioned here as well: #5357) |
@ndreckshage yes, you are correct. I feel like it had some performance benefit as more non-blocking that renderToStaticMarkup but purely anecdotal and dont have anything to back it up. I did find a way to call renderToNodeStream though for certain use cases. If you know your data needs at the request time (the queries that will fire from a react tree), call graphql (using client.query) before I render the react tree then tell apollo to only read from cache serverside. This makes apollo never go into loading state and has data right when useQuery is invoked, allowing renderToNodeStream to work. |
@ndreckshage @JamesCharlesworth you're right you can't use We've been using |
We've found
React.renderToString
/React.renderToStaticMarkup
is a) very expensive and b) blocking, so we're looking to replace it with a non-blocking async version.However, the most costly call site is in
getDataFromTree
due to the recursive nature, so it would be great if Apollo Client would support async render functions -- I don't believe there will be any performance penalty for those using sync functions as it's already running in a microtask.Checklist: