dai-shi / react-suspense-fetch

[NOT MAINTAINED] A low-level library for React Suspense for Data Fetching
MIT License
296 stars 8 forks source link

`cache: stale-while-revalidate` support #12

Closed airhorns closed 2 years ago

airhorns commented 3 years ago

Hey there -- thanks for this awesome example of a render-as-you-fetch library! I understand that it's experimental and new, but, it'd be really awesome to get support for refetching in the background while rendering with the latest fetched version that's available on the client. urql and apollo call this the stale-while-revalidate cache strategy: serve stale data to the react app while revalidating that it is fresh from the server.

I think the current API makes that kind of hard to implement in userland because a fetch is only triggered after an eviction, so you'd have to evict the current data to trigger a refetch, leaving you no data in the meantime to render with.

dai-shi commented 3 years ago

Wow, nice to see having comments on this repo. Happy to discuss.


Does the swr strategy in urql and apollo work in render-as-you-fetch?

AFAIU, in render-as-you-fetch, we get stale state with useTransition.

For refetch, I was probably expecting to use a new param (new object, or new id). Not sure if it's easy to understand and accomplish things DX-wise.

volkanunsal commented 3 years ago

@airhorns The getResult throws a promise, which is caught by Suspense, so if you're using Suspense, that would automatically show the fallback view. I think what would be cool is to have another getResult that doesn't throw the promise, which could be used in a refetch method.

dai-shi commented 3 years ago

Yeah, it should make more sense if refetch doesn't thorw a promise.

dai-shi commented 2 years ago

For now we encourage using useTransition to keep previous state.