Guides
Polling
Refetch a query on an interval while it's on screen with refetchInterval and the polling() plugin.
A query with a refetchInterval refetches every so many milliseconds while a component reads it:
import { 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 } from "~/raf.ts";
export function function postViewsQuery(input: {
slug: string;
}): QueryOptions<readonly ["posts", "views", string], number, number, {
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;
};
}>
postViewsQuery(input: {
slug: string;
}
input: { slug: stringslug: string }) {
return queryOptions<readonly ["posts", "views", string], number, number>(options: BaseOptions<readonly ["posts", "views", string]> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}> & {
readonly queryFn: (context: QueryFunctionContext<readonly ["posts", "views", string], {
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 | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
};
readonly context: {};
readonly options: {
...;
};
}>) => Awaitable<...>;
readonly select?: ((data: number) => number) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions({
BaseOptions<readonly ["posts", "views", string]>.queryKey: readonly ["posts", "views", string]A plain array, usually built by a group(). Handles keep its literal type.
queryKey: posts<readonly ["views", string]>(rest_0: "views", rest_1: string): readonly ["posts", "views", string]posts("views", input: {
slug: string;
}
input.slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", "views", string], {
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;
};
}>) => Awaitable<...>
queryFn: () => function getPostViews(slug: string): Promise<number>getPostViews(input: {
slug: string;
}
input.slug: stringslug),
refetchInterval?: number | false | undefinedrefetchInterval: 2000,
});
}
polling() is the plugin that reads the option, so the client needs it, on its own or through recommended():
const const queryClient: QueryClient<{
readonly "~options": {
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly refetchInterval: number | false | undefined;
};
}>
queryClient = createQueryClient<readonly [Plugin<Polling>]>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly refetchInterval: number | false | undefined;
}>> | undefined;
readonly plugins?: readonly [Plugin<Polling>] | undefined;
}): QueryClient<{
readonly "~options": {
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
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?: readonly [Plugin<Polling>] | undefinedplugins: [function polling(): Plugin<Polling>Refetch read queries every refetchInterval ms.
polling()] });
// or
const const sameWithMore: QueryClient<{
readonly "~options": {
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
};
readonly context: {};
readonly options: {
readonly refetchInterval: number | false | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
};
}>
sameWithMore = createQueryClient<Recommended>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly refetchInterval: number | false | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
}>> | undefined;
readonly plugins?: Recommended | undefined;
}): QueryClient<{
readonly "~options": {
readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
readonly retry: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
};
readonly context: {};
readonly options: {
readonly refetchInterval: number | false | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | 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() });
polling() declares the option, so a factory can set it when its queryOptions, the one the app's raf.ts binds to its client, is bound to a client that has polling(). On any other builder, refetchInterval is a type error.
- Polling starts when the first component reads the query, and stops when the last one unmounts.
- There is one timer per query, however many components read it.
- A poll that lands while another request for the query is in flight joins it instead of starting a second one.
polling()is a feature: it runs while aQueryClientProviderhas the client mounted, so the server never polls.
Each poll is a background refetch: the data on screen stays put. Structural sharing keeps a response equal to the current data from changing the data's identity, so memoized children don't re-render.
The interval per input
The interval is part of the query's options, so a factory can compute it from its input:
export function function commentCountQuery(input: {
slug: string;
live: boolean;
}): QueryOptions<readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}], number, number, {
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;
};
}>
commentCountQuery(input: {
slug: string;
live: boolean;
}
input: { slug: stringslug: string; live: booleanlive: boolean }) {
return queryOptions<readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}], number, number>(options: BaseOptions<readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}]> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}> & {
readonly queryFn: (context: QueryFunctionContext<readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}], {
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 | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
};
readonly context: {};
readonly options: {
...;
};
}>) => Awaitable<...>;
readonly select?: ((data: number) => number) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions({
BaseOptions<readonly ["posts", "commentCount", { slug: string; live: boolean; }]>.queryKey: readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}]
A plain array, usually built by a group(). Handles keep its literal type.
queryKey: posts<readonly ["commentCount", {
slug: string;
live: boolean;
}]>(rest_0: "commentCount", rest_1: {
slug: string;
live: boolean;
}): readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}]
posts("commentCount", input: {
slug: string;
live: boolean;
}
input),
queryFn: (context: QueryFunctionContext<readonly ["posts", "commentCount", {
slug: string;
live: boolean;
}], {
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;
};
}>) => Awaitable<...>
queryFn: () => function getCommentCount(slug: string): Promise<number>getCommentCount(input: {
slug: string;
live: boolean;
}
input.slug: stringslug),
refetchInterval?: number | false | undefinedrefetchInterval: input: {
slug: string;
live: boolean;
}
input.live: booleanlive ? 1000 : false,
});
}false or 0 turns polling off. Each input is its own query, so this picks the interval per query; it can't change while the same query is read.
Polling and staleTime
Polling refetches whatever the data's staleTime: the interval is an explicit request for fresh data. Keep staleTime meaningful anyway, because the loaders that start the same query rely on it.