Skip to content
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

feat(cacheTime): default cacheTime to Infinity for SSR #3377

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/src/pages/guides/migrating-to-react-query-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ This will mostly affect `disabled` queries that don't have any `data` yet, as th

Also, have a look at [the guide on dependent queries](../guides/dependent-queries)

### No _default_ manual Garbage Collection server-side

In v3, React Query would cache query results for a default of 5 minutes, then manually garbage collect that data. This default was applied to server-side React Query as well.

This lead to high memory consumption and hanging processes waiting for this manual garbage collection to complete. In v4, by default the server-side `cacheTime` is now set to `Infinity` effectively disabling manual garbage collection (the NodeJS process will clear everything once a request is complete).

This change only impacts users of server-side React Query, such as with Next.js. If you are setting a `cacheTime` manually this will not impact you (although you may want to mirror behavior).

## New Features 🚀

### Proper offline support
Expand Down
4 changes: 3 additions & 1 deletion docs/src/pages/guides/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ This refetching of stale queries is a perfect match when caching markup in a CDN

### High memory consumption on server

In case you are creating the `QueryClient` for every request, React Query creates the isolated cache for this client, which is preserved in memory for the `cacheTime` period (which defaults to 5 minutes). That may lead to high memory consumption on server in case of high number of requests during that period.
In case you are creating the `QueryClient` for every request, React Query creates the isolated cache for this client, which is preserved in memory for the `cacheTime` period. That may lead to high memory consumption on server in case of high number of requests during that period.

On the server, `cacheTime` defaults to `Infinity` which disables manual garbage collection and will automatically clear memory once a request has finished. If you are explicitly setting a non-Infinity `cacheTime` then you will be responsible for clearing the cache early.

To clear the cache after it is not needed and to lower memory consumption, you can add a call to [`queryClient.clear()`](../reference/QueryClient#queryclientclear) after the request is handled and dehydrated state has been sent to the client.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const queryClient = new QueryClient({

## Set cacheTime to Infinity with Jest

`cacheTime` is set to 5 minutes by default. It means that the cache garbage collector timer will be triggered every 5 minutes. If you use Jest, you can set the `cacheTime` to `Infinity` to prevent "Jest did not exit one second after the test run completed" error message.
If you use Jest, you can set the `cacheTime` to `Infinity` to prevent "Jest did not exit one second after the test run completed" error message. This is the default behavior on the server, and is only necessary to set if you are explicitly setting a `cacheTime`.

## Testing Network Calls

Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/reference/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const result = useQuery({
- The time in milliseconds after data is considered stale. This value only applies to the hook it is defined on.
- If set to `Infinity`, the data will never be considered stale
- `cacheTime: number | Infinity`
- Defaults to `5 * 60 * 1000` (5 minutes)
- Defaults to `5 * 60 * 1000` (5 minutes) or `Infinity` during SSR
- 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 cache times are specified, the longest one will be used.
- If set to `Infinity`, will disable garbage collection
- `queryKeyHashFn: (queryKey: QueryKey) => string`
Expand Down
6 changes: 3 additions & 3 deletions src/core/removable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isValidTimeout } from './utils'
import { isServer, isValidTimeout } from './utils'

export abstract class Removable {
cacheTime!: number
Expand All @@ -19,10 +19,10 @@ export abstract class Removable {
}

protected updateCacheTime(newCacheTime: number | undefined): void {
// Default to 5 minutes if no cache time is set
// Default to 5 minutes (Infinity for server-side) if no cache time is set
this.cacheTime = Math.max(
this.cacheTime || 0,
newCacheTime ?? 5 * 60 * 1000
newCacheTime ?? (isServer ? Infinity : 5 * 60 * 1000)
)
}

Expand Down