You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat(core): StaleTime.Static
* fix: consider StaleTime.Static for invalidation
previously, it didn't matter if we included `stale: true` or not in the filters for refetch, because marking things with query.invalidate() would set them all to stale anyway.
Now, queries with StaleTime.Static will be marked as `invalidated`, but still shouldn't be refetched. The `isStale` filter logic accounts for that, so we have to include it in the filters
* fix: isStale order check
check for observers first because they contain the source of truth calculated with `isStaleByTime`, and it also takes enabled into account
* fix: isStaleByTime logic
we have to check for undefined data first, because queries without data are really always stale; then, the next check must be against StaleTime.Static, because queries that are marked as invalidated are still not stale, even if they are static.
* tests
* fix: type issue in react
* fix: make sure invalidation _always_ only refetches stale queries
this ensures we never refetch Static queries
* fix: never refetch static queries
* fix: make sure we don't refetchOn... for StaleTime.Static even when 'always' is set
* docs: StaleTime.Static
* ref: switch to 'static' string
* docs
Copy file name to clipboardExpand all lines: docs/framework/react/guides/important-defaults.md
+9-2Lines changed: 9 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,20 @@ Out of the box, TanStack Query is configured with **aggressive but sane** defaul
9
9
10
10
> To change this behavior, you can configure your queries both globally and per-query using the `staleTime` option. Specifying a longer `staleTime` means queries will not refetch their data as often
11
11
12
+
- A Query that has a `staleTime` set is considered **fresh** until that `staleTime` has elapsed.
13
+
14
+
- set `staleTime` to e.g. `2 * 60 * 1000` to make sure data is read from the cache, without triggering any kinds of refetches, for 2 minutes, or until the Query is [invalidated manually](./query-invalidation.md).
15
+
- set `staleTime` to `Infinity` to never trigger a refetch until the Query is [invalidated manually](./query-invalidation.md).
16
+
- set `staleTime` to `'static'` to **never** trigger a refetch, even if the Query is [invalidated manually](./query-invalidation.md).
17
+
12
18
- Stale queries are refetched automatically in the background when:
13
19
- New instances of the query mount
14
20
- The window is refocused
15
21
- The network is reconnected
16
-
- The query is optionally configured with a refetch interval
17
22
18
-
> To change this functionality, you can use options like `refetchOnMount`, `refetchOnWindowFocus`, `refetchOnReconnect` and `refetchInterval`.
23
+
> Setting `staleTime` is the recommended way to avoid excessive refetches, but you can also customize the points in time for refetches by setting options like `refetchOnMount`, `refetchOnWindowFocus` and `refetchOnReconnect`.
24
+
25
+
- Queries can optionally be configured with a `refetchInterval` to trigger refetches periodically, which is independent of the `staleTime` setting.
19
26
20
27
- Query results that have no more active instances of `useQuery`, `useInfiniteQuery` or query observers are labeled as "inactive" and remain in the cache in case they are used again at a later time.
21
28
- By default, "inactive" queries are garbage collected after **5 minutes**.
Copy file name to clipboardExpand all lines: docs/framework/react/reference/useQuery.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,12 +90,13 @@ const {
90
90
- This function receives a `retryAttempt` integer and the actual Error and returns the delay to apply before the next attempt in milliseconds.
91
91
- A function like `attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)` applies exponential backoff.
92
92
- A function like `attempt => attempt * 1000` applies linear backoff.
93
-
-`staleTime: number | ((query: Query) => number)`
93
+
-`staleTime: number | 'static' ((query: Query) => number | 'static')`
94
94
- Optional
95
95
- Defaults to `0`
96
96
- The time in milliseconds after which data is considered stale. This value only applies to the hook it is defined on.
97
-
- If set to `Infinity`, the data will never be considered stale
97
+
- If set to `Infinity`, the data will not be considered stale unless manually invalidated
98
98
- If set to a function, the function will be executed with the query to compute a `staleTime`.
99
+
- If set to `'static'`, the data will never be considered stale
99
100
-`gcTime: number | Infinity`
100
101
- Defaults to `5 * 60 * 1000` (5 minutes) or `Infinity` during SSR
101
102
- The time in milliseconds that unused/inactive cache data remains in memory. When a query's cache becomes unused or inactive, that cache data will be garbage collected after this duration. When different garbage collection times are specified, the longest one will be used.
@@ -116,21 +117,21 @@ const {
116
117
- Defaults to `true`
117
118
- If set to `true`, the query will refetch on mount if the data is stale.
118
119
- If set to `false`, the query will not refetch on mount.
119
-
- If set to `"always"`, the query will always refetch on mount.
120
+
- If set to `"always"`, the query will always refetch on mount (except when `staleTime: 'static'` is used).
120
121
- If set to a function, the function will be executed with the query to compute the value
This function returns a promise that will resolve when all of the queries are done being refetched. By default, it **will not** throw an error if any of those queries refetches fail, but this can be configured by setting the `throwOnError` option to `true`
392
393
394
+
**Notes**
395
+
396
+
- Queries that are "disabled" because they only have disabled Observers will never be refetched.
397
+
- Queries that are "static" because they only have Observers with a Static StaleTime will never be refetched.
398
+
393
399
## `queryClient.cancelQueries`
394
400
395
401
The `cancelQueries` method can be used to cancel outgoing queries based on their query keys or any other functionally accessible property/state of the query.
0 commit comments