Skip to content

raf-query is early. The API can still change between minor releases, so use it at your own risk.

raf-query

Guides

Window focus refetching

Refetch stale queries on screen when the user comes back to the tab or reconnects, with focusRefetch() and reconnectRefetch(), and opt queries out.

When the user leaves your app and comes back, the data on screen may be out of date. Two plugins refetch it:

  • focusRefetch() refetches every read, stale query when the window regains focus.
  • reconnectRefetch() does the same when the browser comes back online.

Neither is on by default. Add them to the client, or pass recommended(), which has both:

const 
const queryClient: QueryClient<{
    readonly "~options": {
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
    };
}>
queryClient
=
createQueryClient<readonly [Plugin<FocusRefetch>, Plugin<ReconnectRefetch>]>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
    readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
    }>> | undefined;
    readonly plugins?: readonly [Plugin<FocusRefetch>, Plugin<ReconnectRefetch>] | undefined;
}): QueryClient<...>

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<FocusRefetch>, Plugin<ReconnectRefetch>] | undefinedplugins: [function focusRefetch(): Plugin<FocusRefetch>

Refetch read, stale queries when the window regains focus, except those with refetchOnWindowFocus: false.

focusRefetch
(), function reconnectRefetch(): Plugin<ReconnectRefetch>

Refetch read, stale queries when the browser comes back online, except those with refetchOnReconnect: false.

reconnectRefetch
()],
}); // or const
const withEverything: QueryClient<{
    readonly "~options": {
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly retry: number | false | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
withEverything
=
createQueryClient<Recommended>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
    readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly retry: number | false | undefined;
        readonly refetchInterval: number | false | undefined;
    }>> | undefined;
    readonly plugins?: Recommended | undefined;
}): QueryClient<{
    readonly "~options": {
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly retry: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly retry: number | false | 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(): Recommended

The 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
() });

Only queries that a component is reading refetch, and only stale ones: data within its staleTime stays as it is. With the default staleTime of 0, that's everything on screen.

Both are features: they run while a QueryClientProvider has the client mounted, so they only ever run in the browser.

Turning it off

For every query, leave the plugin out of the client:

// `recommended()` without focusRefetch() and reconnectRefetch()
const 
const withoutFocus: QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchInterval: number | false | undefined;
    };
}>
withoutFocus
=
createQueryClient<readonly [Plugin<Retry>, Plugin<Polling>]>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
    readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
        readonly retry: number | false | undefined;
        readonly refetchInterval: number | false | undefined;
    }>> | undefined;
    readonly plugins?: readonly [Plugin<Retry>, Plugin<Polling>] | undefined;
}): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | ... 1 more ... | undefined;
        readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | 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?: readonly [Plugin<Retry>, Plugin<Polling>] | undefinedplugins: [function retry(options?: RetryOptions): Plugin<Retry>

Retry failed requests. Thrown Responses (router redirects and not-founds) and cancelled requests aren't retried, server clients never retry, and a browser that is offline waits until it's back online before retrying. A query sets its own attempts with retry.

retry
(), function polling(): Plugin<Polling>

Refetch read queries every refetchInterval ms.

polling
()] });

For one query, set refetchOnWindowFocus: false or refetchOnReconnect: false in its factory. The plugins declare these options, so a factory can set them when its queryOptions, the one the app's raf.ts binds to its client, is bound to a client that has them:

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";
// A post's body rarely changes while someone reads it. export function
function postBodyQuery(input: {
    slug: string;
}): QueryOptions<readonly ["posts", "body", string], Post, Post, {
    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;
    };
}>
postBodyQuery
(
input: {
    slug: string;
}
input
: { slug: stringslug: string }) {
return
queryOptions<readonly ["posts", "body", string], Post, Post>(options: BaseOptions<readonly ["posts", "body", 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", "body", 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: Post) => Post) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", "body", string]>.queryKey: readonly ["posts", "body", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
: posts<readonly ["body", string]>(rest_0: "body", rest_1: string): readonly ["posts", "body", string]posts("body",
input: {
    slug: string;
}
input
.slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", "body", 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 getPost(slug: string): Promise<Post>getPost(
input: {
    slug: string;
}
input
.slug: stringslug),
refetchOnWindowFocus?: boolean | undefinedrefetchOnWindowFocus: false, }); }

The client's defaults take the same options, for every query that doesn't set its own. A long staleTime is often the better fix: the data then refetches on focus only once it's actually old.

What counts as focus

focusRefetch() listens for two events:

  • the document's visibilitychange event, when document.visibilityState becomes "visible": switching back to the tab, restoring the window, unlocking the phone;
  • the window's focus event, while the page is visible: clicking back into the browser window from another app.

reconnectRefetch() listens for the window's online event.

To test it by hand, switch tabs and come back. See Testing for firing these events in tests.