TanStack Query
The key is query.url(), the query function calls .get(). TanStack Query passes an
AbortSignal, which headquery forwards to fetch, so cancelled queries abort the request.
import { } from "@tanstack/react-query";
const = .("blog").("-date").(10).(["title", "slug", "date"]);
export function () {
return ({
: ["headquery", .()],
: ({ }) => .({ }),
});
}
const { const data: Paginated<Pick<BlogEntry, "date" | "slug" | "title">> | undefinedThe data from before the failed refetch, which is kept.
data } = ();
A reusable helper
Wrap this in queryOptions once, then use it with useQuery, useSuspenseQuery or for
prefetching on the server:
import { } from "@tanstack/react-query";
import type { RunOptions } from "@headquery/statamic";
interface <> {
(): string;
(?: RunOptions): <>;
}
export function <>(: <>) {
return ({
: ["headquery", .()],
: ({ }) => .({ }),
});
}
const = .("settings");
(());
await .(());Pagination
Keep the base query without a page, so its URL stays a stable key, and add the page in the query function:
import { } from "@tanstack/react-query";
const = .("blog").("-date").(10);
({
: ["headquery", .()],
: 1,
: ({ , }) => .().({ }),
: () =>
.. < (.. ?? 0) ? .. + 1 : ,
});Retries
Errors are HeadqueryHttpError with a status, so client errors can skip retries:
import { } from "@headquery/statamic";
const = new ({
: {
: {
: (, ) =>
!( instanceof && . < 500) && < 3,
},
},
});Last updated on