diff --git a/packages/svelte-query/src/createPrefetchInfiniteQuery.ts b/packages/svelte-query/src/createPrefetchInfiniteQuery.ts new file mode 100644 index 0000000000..3c812f6419 --- /dev/null +++ b/packages/svelte-query/src/createPrefetchInfiniteQuery.ts @@ -0,0 +1,24 @@ +import { useQueryClient } from "./useQueryClient.js"; +import type { DefaultError, FetchInfiniteQueryOptions, QueryKey } from "@tanstack/query-core"; + +export function createPrefetchInfiniteQuery< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, + TPageParam = unknown, +>( + options: FetchInfiniteQueryOptions< + TQueryFnData, + TError, + TData, + TQueryKey, + TPageParam + >, +) { + const queryClient = useQueryClient() + + if (!queryClient.getQueryState(options.queryKey)) { + queryClient.prefetchInfiniteQuery(options) + } +} diff --git a/packages/svelte-query/src/createPrefetchQuery.ts b/packages/svelte-query/src/createPrefetchQuery.ts new file mode 100644 index 0000000000..d450b72a0e --- /dev/null +++ b/packages/svelte-query/src/createPrefetchQuery.ts @@ -0,0 +1,15 @@ +import { useQueryClient } from "./useQueryClient.js"; +import type { DefaultError, FetchQueryOptions, QueryKey } from "@tanstack/query-core"; + +export function createPrefetchQuery< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +>(options: FetchQueryOptions) { + const queryClient = useQueryClient() + + if (!queryClient.getQueryState(options.queryKey)) { + queryClient.prefetchQuery(options) + } +} diff --git a/packages/svelte-query/src/index.ts b/packages/svelte-query/src/index.ts index 735cd89f59..39cecb8a50 100644 --- a/packages/svelte-query/src/index.ts +++ b/packages/svelte-query/src/index.ts @@ -8,6 +8,7 @@ export * from './types.js' export * from './context.js' export { createQuery } from './createQuery.js' +export { createPrefetchQuery } from './createPrefetchQuery.js' export type { QueriesResults, QueriesOptions } from './createQueries.js' export type { DefinedInitialDataOptions, @@ -16,6 +17,7 @@ export type { export { queryOptions } from './queryOptions.js' export { createQueries } from './createQueries.js' export { createInfiniteQuery } from './createInfiniteQuery.js' +export { createPrefetchInfiniteQuery } from './createPrefetchInfiniteQuery.js' export { infiniteQueryOptions } from './infiniteQueryOptions.js' export { createMutation } from './createMutation.js' export { useMutationState } from './useMutationState.js'