-
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
Re-queries on hydration after ssr using renderToPipeableStream #11319
Comments
Hi there, lots of things to unpack here! Unfortunately, you've fallen into a big pitfall the React team hasn't really documented well: there is currently no data fetching story for You can do the workaround you are currently doing - running I have recently talked with Josh Story about this, and there is a good chance that React 19 might include a few helpers that would be necessary for us to get this working - but as of right now, there is just no React API for us to inject into the stream, so we cannot support this. TLDR: as of right now, we cannot support |
I was stuck in the same problem for several days. I hope the Apollo team can provide examples of suggested SSRs now that as with libraries in general, they have an examples directory. @phryneas Do you have any suggestions what is the best current method for building SSR with Apollo? is it still renderToStaticMarkup like before? |
@kcoet as I wrote above, It sucks, but we cannot change it. I have a working prototype of hooks that I added myself to the React core to make this work, but of course we won't ask you to patch your As it stands, just stay clear of |
Small update on this - this is still difficult to set up, but not impossible. I'm currently in the process of writing docs on this, you can follow the PR in #11807 - I'd be happy for any feedback :) |
For anyone interested, I hacked around this by only sending the So basically... by not streaming, but works for my use case since I'm just migrating from React 17 + Loadable Components and I needed Sharing this in case someone is in a migration like me and got scared by this thread... there's hope! Can't wait to have proper streaming implemented. |
I'd love to see an update on this. The documentation is very misleading, as React says very explicitly that I think it would be very helpful to have this ironed out in the docs. Thanks for your hard work |
Neither does @moishinetzer have you taken a look at this page from the PR I linked to above? The whole story seems to move too quickly right now to write "final" documentation on it, but what I wrote there should help you set up something with the necessary workarounds until React itself finally gets to a state where this is supported. |
Thanks for the swift response! I'll give it a shot and update here on the thread. My context is that we have a really large production react app and we're trying to migrate from Webpack with loadable (to split up our routes), to Vite with lazy loading. However, lazy loading (as the user above mentioned) is only supported with the streaming APIs |
@moishinetzer then you probably have to go with the setup in the documentation that I linked to. That said, I remember setting up streaming SSR with Vite to be quite the beast. You might want to look into something like Vike - they also have an Apollo Client extension based on our streaming package. |
The truth is that we don't really care for the In fact, we actually like the UX of making the user wait until the page has fully loaded, without any spinners rendering etc. even if it's a faster TTFB. I can use the function renderToStringWithLazy(reactNode: React.ReactNode): Promise<string> {
return new Promise((resolve: (html: string) => void, reject) => {
// Collect all chunks into this string
let html = ''
// Create a writable stream that concatenates chunks
const writableStream = new Writable({
write(chunk, encoding, callback) {
html += chunk
callback()
},
})
// Track error state
let didError = false
let error: Error | null = null
const { pipe } = renderToPipeableStream(reactNode, {
onShellError(err) {
didError = true
error = err as Error
reject(error)
},
onError(err) {
didError = true
error = err as Error
console.error('Error during rendering:', err)
},
// We use onAllReady instead of onShellReady to ensure all lazy/suspended content is loaded
onAllReady() {
try {
// If we encountered an error earlier, don't proceed
if (didError) {
return reject(error)
}
// Pipe the complete content to our writable stream
pipe(writableStream)
// When the stream is done, resolve with the complete HTML
writableStream.on('finish', () => {
resolve(html)
})
writableStream.on('error', (err) => {
reject(err)
})
} catch (err) {
reject(err)
}
},
})
setTimeout(() => {
reject(new Error(`Rendering timed out after 10 seconds`))
}, 10_000)
})
} |
That's the thing. SSR is one render. That means with the classical hooks you render a loading state and that's it. What we did in the past was restart that render after all network requests finished, until no more network requests were started by Apollo Hooks, and see that as "the end of the render", and that would be what you'd have to reimplement: But you can see how that always was a suboptimal solution now that React can just "pause" rendering a component and waiting for the finished result with suspense - resulting in only one render of the tree. |
I see, so for now I should be able to go ahead with my method above so long as I change all my queries to use the |
Issue Description
version: 3.8.*
To begin with, it is better to consider the problem using an example:
Example with useSuspenseQuery & renderToPipeableStream
https://github.com/iamafansev/test-suspense-query
In this example, the
fetchPolicy
is set tocache-and-network
.We expect that there will be no duplicate requests during hydration since we have a valid cache.
However, we see repeated outgoing requests in the developer tools.
At the same time, if you set the caching policy to
cache-first
, then repeated requests are not executed. This confirms that the cache contains correct data and the request need not be executed.When I encountered this problem, I thought that the problem was only when using new features such as
useSuspenseQuery
, but I have another example:Example with
useQuery / getDataFromTree & renderToPipeableStream
(workaround to use React.(Suspense / lazy))https://github.com/iamafansev/use-query-with-lazy-ssr
There is some workaround here to use pause rendering just to wait for lazy components. In the server-side implementation, we first traverse the tree using
getDataFromTree
and only after that, we pass the tree torenderToPipeableStream
.Similar to the first example, a repeated request is performed here with the cache-and-network caching policy and is not performed with cache-first.
At the same time, if you do not use
renderToPipeableStream
for the example withuseQuery
, then repeated requests will not be executed under thecache-and-network
caching policy.The problem also appears in version 3.7.17
Link to Reproduction
https://github.com/iamafansev/test-suspense-query
Reproduction Steps
The text was updated successfully, but these errors were encountered: