Skip to content

Clarify online support #2927

@TkDodo

Description

@TkDodo

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 to false.

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 to true will be beneficial because "fetching" from the browser-cache will also work when you're offline.

This would mean that we would, if offline is false (the default) and you have no internet:

  • pause retries when you have no internet. This would be non-breaking because we already do that.
  • Do not trigger refetches. Currently, we only do this for window focus refetches, but manual calls to refetch / invalidation don't adhere to this. If a query is explicitly refetched, it should probably be marked as stale instead so that it will be refetched on reconnect.
  • When a new query mounts, stay in whatever current state and don't trigger background refetches.
    • This means that if a query mounts for the first time, it will be in idle state - we need to properly communicate this that people need to handle that.
    • if a query is already in error state, we also wouldn't refetch, but stay in error state instead.

if offline is true, we would:

  • don't pause retries because you are offline - they would just continue. if you are not fetching via network, it would not be impacted, and if you have cached data in the browser cache, it would succeed on the first try anyways.
  • trigger refetches normally - same reasoning as above
  • when a new query mounts - also fetch normally (same reasoning as above)

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions