feat(query-core)!: make server timers opt-in #12715
Showing 22 changed files with 9 additions and 34 deletions (loading 19).
x
@@ -0,0 +1,6 @@
+---
+'@tanstack/query-core': major
+'@tanstack/query-async-storage-persister': patch
+---
+
+The default timeout provider skips timers on the server, even with finite `gcTime` or explicit `retry` options. A long-lived server can install a custom provider with `timeoutManager.setTimeoutProvider()`. With a provider that schedules timers, unused entries expire after five minutes by default. Its scheduling methods can return `undefined` to skip a timer. The async storage persister uses a platform timer if the provider skips a delay, so queued writes still finish.
@@ -489,37 +489,6 @@
-On the server, `gcTime` defaults to `Infinity`. With `Infinity`, no garbage collection timer is scheduled, and the cache is freed from memory together with the `QueryClient` once the request finishes.
+Use a request-scoped `QueryClient` for SSR. By default, Query skips garbage collection timers on the server, even when a query sets a finite `gcTime`.
-Any finite `gcTime` schedules a timer for each query, and that timer keeps the query in memory until it fires. In Node.js, a timer also keeps alive the [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) context it was created in. If your server framework stores the request in that context, the request stays in memory even after the response is sent, until the timer fires.
-
-This is easy to miss: a `gcTime` set in `defaultOptions` applies on the server too, so a value intended for the client also replaces `Infinity` on the server.
-
-```tsx
-const queryClient = new QueryClient({
- defaultOptions: {
- queries: {
- // This also applies on the server
- gcTime: 1000 * 60,
- },
- },
-})
-```