@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
45 lines (44 loc) • 1.39 kB
TypeScript
import type { TPrefetchOptions, TQueryFetcher, TQueryKey } from "./types.js";
/**
* Prefetch query data before component mounts
* Populates cache for later use by query()
*
* Unlike query(), prefetchQuery doesn't create a reactive store.
* It simply loads data into the cache for instant availability when query() is called.
*
* @param key Query key to prefetch
* @param fetcher Async function to fetch data
* @param options Prefetch configuration options
* @returns Promise that resolves when prefetch completes (or fails silently)
*
* @example
* Basic usage:
* ```typescript
* // Prefetch on route change
* router.beforeEach(async (to) => {
* if (to.path === '/users') {
* await prefetchQuery('users', fetchUsers);
* }
* });
*
* // Later, component mounts
* const usersQuery = query('users', fetchUsers);
* // → isLoading = false (cache hit!)
* // → data = [prefetched users]
* ```
*
* @example
* With staleTime:
* ```typescript
* // Only prefetch if data is stale
* await prefetchQuery('users', fetchUsers, { staleTime: 5000 });
* ```
*
* @example
* Force refetch:
* ```typescript
* // Always fetch regardless of cache
* await prefetchQuery('users', fetchUsers, { force: true });
* ```
*/
export declare function prefetchQuery<TData = unknown>(key: TQueryKey, fetcher: TQueryFetcher<TData>, options?: TPrefetchOptions): Promise<void>;