exhibitionist-digital / ultra

Zero-Legacy Deno/React Suspense SSR Framework

Home Page:https://ultrajs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useAsync returns the callback if hydration occurs before the data arrives

b3nten opened this issue · comments

If the data being streamed to the client arrives after hydration, the callback is returned. In most use-cases, this will mean that the data is fetched again from the client, and the streamed data is ignored.

useAsync could expose an option to either return the callback, or a promise that waits for the data to be streamed in, and the user can decide. A common usecase would be to have returnCallback set to false when the component mounts, so it either returns the streamed data or a promise to return the streamed data. Then returnCallback is set to true for subsequent calls, so subsequent requests are called rather than returning the stale streamed data. This would replace ssrOnly.

This leaves an edge case where, for example, with client side routing, useAsync is called without any streaming. It wouldn't make sense to have returnCallback be true here, so it's either handled internally here or we create a hook that handles this state. For example, some sort of useGlobalMount which returns true once the app has mounted, thus always returning the callback except for the first render.

Here is an example of what useAsync could look like with this pattern.

I suppose this is fixed by #166