Guides
Important defaults
When raf-query fetches, refetches, retries and garbage-collects, which behaviors are on by default, and which options change it.
A client from createQueryClient() caches, dedupes and garbage-collects, and nothing else. Retries, refetching on focus and reconnect, and polling are plugins you give the client. recommended() returns the ones most apps want:
export function function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient() {
return createQueryClient<Recommended>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}>> | undefined;
readonly plugins?: Recommended | undefined;
}): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | ... 1 more ... | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
};
}>
Create a query client. Behaviors are opt-in: pass plugins, such as
recommended(). The client's root is what they declare.
createQueryClient({
plugins?: Recommended | undefinedplugins: function recommended(): RecommendedThe behaviors most apps want: retries, refetching on focus and reconnect,
and polling. Pass it as the client's plugins, or spread it among your own:
recommended(),
defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}>> | undefined
defaults: { staleTime?: number | undefinedHow long data stays fresh, in ms. Default 0.
staleTime: 30_000 },
});
}| Plugin | Kind | What it does |
|---|---|---|
retry() | link | Retries failed requests. See Retries |
focusRefetch() | feature | Refetches read, stale queries when the window regains focus |
reconnectRefetch() | feature | Refetches read, stale queries when the browser comes back online |
polling() | feature | Refetches read queries on a timer set by refetchInterval |
A link wraps every request; a feature runs while a provider has the client mounted. recommended() is an array: pass plugins yourself to pick some of them, or spread it among your own, plugins: [...recommended(), myPlugin].
Each plugin declares the per-query options it reads, such as retry and refetchInterval. A factory can set them because its queryOptions is the one the app binds to that client, once, in a file of its own:
import {
function bindInfiniteQueryOptions<T = NoDecl>(): InfiniteQueryOptionsBuilder<RootOf<T>, SetsOf<T>>Makes infiniteQueryOptions, the builder of a paged query factory's
options, bound like bindQueryOptions. Bind it next to queryOptions,
in apps that page:
bindInfiniteQueryOptions,
function bindQueryOptions<T = NoDecl>(): QueryOptionsBuilder<RootOf<T>, SetsOf<T>>Makes queryOptions, the builder of a query factory's options, bound to
the clients its factories run on:
bindQueryOptions,
function bindUseQueryClient<T>(): () => QueryClient<RootOf<T>>useQueryClient typed with a root, so verbs called with the client it
returns check factories and targets against that root. T is the
client's type, or the type of a function that makes it. Bind once per app:
bindUseQueryClient,
} from "@raf-query/react";
import type { function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient } from "./query-client.ts";
export const const queryOptions: QueryOptionsBuilder<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}, {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
queryOptions = bindQueryOptions<() => QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>(): QueryOptionsBuilder<...>
Makes queryOptions, the builder of a query factory's options, bound to
the clients its factories run on:
bindQueryOptions<typeof function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient>();
export const const infiniteQueryOptions: InfiniteQueryOptionsBuilder<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}, {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
infiniteQueryOptions =
bindInfiniteQueryOptions<() => QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>(): InfiniteQueryOptionsBuilder<...>
Makes infiniteQueryOptions, the builder of a paged query factory's
options, bound like bindQueryOptions. Bind it next to queryOptions,
in apps that page:
bindInfiniteQueryOptions<typeof function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient>();
export const const useQueryClient: () => QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
useQueryClient = bindUseQueryClient<() => QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>(): () => QueryClient<...>
useQueryClient typed with a root, so verbs called with the client it
returns check factories and targets against that root. T is the
client's type, or the type of a function that makes it. Bind once per app:
bindUseQueryClient<typeof function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient>();Beside the plugins' options, every builder takes the core's: staleTime, gcTime, structuralSharing and ssr. Plugins covers binding, and writing plugins of your own.
Keep these in mind as you read the rest of the guides. Where they mention retries, focus and reconnect refetching, or polling, they assume recommended():
-
Reading a query never refetches stale data.
useQueryreads what's in the cache: a component that mounts with stale data renders it and starts no request, unlike TanStack Query'suseQuery, which refetches stale data on mount. Loaders decide freshness: every load entry refetches stale data when a loader starts it (prefetchandswrin the background,revalidatebefore it resolves). Stale data refreshes:- when the next loader or server component starts the query with
load, which happens on every navigation to the page - when the query is invalidated, such as at the end of an action that writes data
- on window focus and on reconnect, for queries being read, with
focusRefetch()andreconnectRefetch() - on a timer, with
polling()and a query'srefetchInterval(Polling)
Rendering the same handle in ten components reads one cache entry.
- when the next loader or server component starts the query with
-
Cached data is stale immediately (
staleTime: 0). -
Polling is per query.
refetchIntervalrefetches on a timer while a component reads the query, independent ofstaleTime. See Polling. -
Unread entries are kept for five minutes (
gcTime: 300_000). An entry nobody reads is inactive: it stays in the cache in case it's needed again, then is garbage-collected. -
Failed requests retry three times, with a delay that doubles from one second, up to 30 seconds, before the error reaches the UI. Retries wait while the browser is offline. Thrown
Responses aren't retried. -
Data is structurally shared. When a refetch returns data that's deeply equal to what's cached, the cache keeps the old objects, so unchanged parts keep their identity and nothing re-renders. See Render optimizations.
On the server
A server client (one per request) renders once, so it behaves differently:
- Cached data is reused, whatever
staleTimesays: what the request fetched is fresh for that request, and a refetch could only repeat a request whose result is never shown.revalidateentries refetch invalidated data, so they see a write made earlier in the request. - No retries, even with
retry(), and no garbage collection: the cache lives as long as the request. - No features run. A provider starts them when it mounts, which only happens in the browser.
useIsFetching()returns0, so loading indicators never appear in server HTML.
See SSR and hydration.
Changing the defaults
defaults sets an option for every query of the client: the core's staleTime, gcTime and structuralSharing, and the options the client's plugins declare. A query's own option, set in its factory, wins:
const const queryClient: QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
queryClient = createQueryClient<Recommended>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}>> | undefined;
readonly plugins?: Recommended | undefined;
}): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | ... 1 more ... | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
};
}>
Create a query client. Behaviors are opt-in: pass plugins, such as
recommended(). The client's root is what they declare.
createQueryClient({
plugins?: Recommended | undefinedplugins: function recommended(): RecommendedThe behaviors most apps want: retries, refetching on focus and reconnect,
and polling. Pass it as the client's plugins, or spread it among your own:
recommended(),
defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}>> | undefined
defaults: {
staleTime?: number | undefinedHow long data stays fresh, in ms. Default 0.
staleTime: 30_000,
gcTime?: number | undefinedHow long an entry with no reader stays cached. Default 5 minutes; never on the server.
gcTime: 10 * 60_000,
// Options of `retry()` and `focusRefetch()`
retry?: number | false | undefinedretry: 1,
refetchOnWindowFocus?: boolean | undefinedrefetchOnWindowFocus: false,
},
});| Option | Where | Default |
|---|---|---|
staleTime | Client or query | 0 ms |
gcTime | Client or query | 5 minutes; never on the server |
structuralSharing | Client or query | true |
retry({ attempts, delay, when }) | The plugin | 3 attempts, doubling from 1 s up to 30 s, every error but a thrown Response |
retry | Client or query, with retry() | The plugin's attempts; false turns retries off |
refetchInterval | Client or query, with polling() | None |
refetchOnWindowFocus | Client or query, with focusRefetch() | true |
refetchOnReconnect | Client or query, with reconnectRefetch() | true |
Watch the defaults at work in Caching, which has a live demo of an entry going from fresh to stale to garbage-collected.