-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Clarify online support #2927
Comments
one thing that is not addressed with the mentioned approach is the following scenario:
--> your loading spinner will show until you're online again, even though you're not technically "fetching" two solutions come to mind:
What do you think @arnaudbzn ? |
Ah, another good way would be to maybe expose the internal |
I prefer this solution! A new
|
spinning this idea a bit further:
we could of course provide it would mean that if a new query mounts that doesn't have any data yet, if you are offline, instead of putting it in the
sure, if you just display a loading spinner based on the status, you would see the spinner, but I think this is better than putting the query in similar, if you have data already and mount a new query, the
I think we could do that, too.
could you elaborate a bit on how that could look like? I'm still not totally happy with the fact that you have to decide between online / offline queries. For example, you can't know if a query will yield data from the browser cache or not. If it does, mounting it, fetching + getting data from the browser cache would work, but with the initial proposal, you would have to specify it as an |
I can have one query for the AsyncStorage and another one for an API. The same concept of offline cannot apply for both. |
I think this is exactly the case I wanted to solve with the The onlineManager is a global singleton, which doesn't have any correlation to queries. You either are online, or you're not (I think...) |
The naming could be more precise, instead of
|
yes, definitely agree. I like |
|
What about I imagine this would be able to be set as a config value in QueryClient and it would be rare to override that value on a per query basis. This means the verboseness of the name is less of an issue |
|
Just for clarification: |
@arnaudbzn it can. I have since refined the api again, and almost have a PR ready. My current solution is:
I'll also write docs on it, but general,
hope that makes sense and covers all cases. I've also combined |
Great! |
@TkDodo really well thought! I cant wait to try this as I'm a big fan of offline-first apps. |
Hello there, I'm very curious as to this feature...so much so that I am testing it out in my test/internal work app. Is the goal of the persistentQueryClient to auto-dehydrate/hydrate the data on connectivity change? Or is this something that would need to be setup on a per basis use case? Currently I'm trying the alpha release for this purpose soley, as I have the condition that a user may want to submit (post/mutate) data whilst their offline. If they get internet back while their still in app, great, I can just merge errors on resulting onSuccess callback (that will be missing the data)...but what about if they don't and they restart the app with internet, this would be where I find any existing "persisted" cache, and hydrate it before first fetch. I apologize for my language, I'm a self taught everything-related-to-this guy. Thanks! |
awesome, thank you for your feedback 🙌
no, the this can be used at the start of the app to keep the localStorage in sync with your app, and to initially hydrate it. as of today, on v4, we have some more options:
this should give you even more control over when to read from / write to an external storage.
for this case, I think you need to |
Awesome, yes I'm doing that currently. I suppose I'm wishfully thinking I can extend this new persistence feature to cover a sudo "offline sync" function. I see now it's complicated enough that services cover this space, like Realm. My user base isn't large enough to warrant that yet, though. I think if I were to utilize the newest, v4 persist features you mentioned along with possibly extending the class to listen to the onlineManager, and when offline switch my api url to that of a local database that I then update and cache until the internet is back on. Then I'd have to implement a sync feature with lastUpdated-at type fields to try and merge. I think it's only doable for me now this way as I have control over the front/back and have less than 50 users. If my gibberish anove makes any sense, please feel free to offer advice on my idea. Anyway. Great job on the library. It's in my template I start with on every new build! |
Hi! Version: 4.0.0-alpha.20 Just installed the new version in purpose of using 'networkMode': 'online' so refetching doesn't happen when Internet connection is lost (also it is important to note that the query didn't start nor is in loading/fetching state when connection is lost - it is idled - and it is also not mounting for the first time). The problem is that for some reason as soon as I turn off connection on real device, isFetching flag turns to 'true' for like 1-2 seconds and then goes to 'false'. Is this the intended behaviour? By "queries will not start or refetch unless you have internet" I would say it is not. Despite that, how to tackle the fact that on the first mount query starts loading even though there is no Internet connection? Thanks! |
@morismoze thanks for trying out v4!
No, it should not. Can you reproduce this in a codesandbox, or in an expo snack?
When a query mounts for the first time and you have no internet, it should be in |
Managed to work it out with isPaused flag, thank You!
Tried with both but can't reproduce wanted behaviour. Also forgot to mention that I'm using https://react-query.tanstack.com/react-native#online-status-management and https://react-query.tanstack.com/react-native#refetch-on-app-focus, but I think those two shouldn't have any influence on that particular issue? I'll try to explain the case once more (since it is the only way): I'm on Home screen, everything is fetched, so there is no fetching going on - everything is idled. Then I shut off mobile data and isFetching turns to true for 1-2 seconds and then falls back to false |
👍
I cannot imagine isFetching going to |
We currently have multiple issues pertaining to online / offline support that are hard to fix without breaking something:
Proposed solution
Create a new query flag for
offline
queries (offline: boolean
- name to be discussed). Since react-query is primarily an async state manager, it actually doesn't care about "being online". However, most people will use react-query for data fetching, so it makes sense to default this tofalse
.If you are using react-query to fetch from somewhere else, like AsyncStorage, where fetches should also happen when you are offline, you can set this flag to
true
. Also, if your api supports http caching, setting this totrue
will be beneficial because "fetching" from the browser-cache will also work when you're offline.This would mean that we would, if
offline
isfalse
(the default) and you have no internet:refetch
/invalidation
don't adhere to this. If a query is explicitly refetched, it should probably be marked asstale
instead so that it will be refetched on reconnect.idle
state - we need to properly communicate this that people need to handle that.error
state, we also wouldn't refetch, but stay in error state instead.if
offline
is true, we would:If you say that your query is an offline query, but you are still fetching over the internet and have no browser cache, you will get a failed Promise after 3 retries and have to handle the error properly.
This new flag could potentially replace the
refetchOnReconnect
flag. If a query is an offline query, it wouldn't refetch on reconnect, and if it's an online query, it would. If we want to keep the flag, we could default it to the value of!offline
. This is still to be discussed.The text was updated successfully, but these errors were encountered: