UNPKG

preact-missing-hooks

Version:

A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.

45 lines (44 loc) 1.36 kB
export interface UsePrefetchOptions { /** * Resource type for prefetch. * - "document": uses <link rel="prefetch"> (default, for next navigation) * - "fetch": uses fetch() to warm the HTTP cache (e.g. for API or same-origin data) */ as?: "document" | "fetch"; } export interface UsePrefetchReturn { /** Prefetch a URL so it is cached for later use. No-op if URL was already prefetched or empty. */ prefetch: (url: string, options?: UsePrefetchOptions) => void; /** Check whether a URL has already been prefetched in this hook instance. */ isPrefetched: (url: string) => boolean; } /** * A Preact hook that returns a stable prefetch function to preload URLs (documents or data) * so they are cached before the user navigates or needs them. Useful for link hover or * route preloading. * * @returns Object with prefetch(url, options?) and isPrefetched(url) * * @example * ```tsx * function NavLink({ href, children }) { * const { prefetch } = usePrefetch(); * return ( * <a * href={href} * onMouseEnter={() => prefetch(href)} * > * {children} * </a> * ); * } * ``` * * @example * ```tsx * // Prefetch API data * const { prefetch } = usePrefetch(); * prefetch('/api/user', { as: 'fetch' }); * ``` */ export declare function usePrefetch(): UsePrefetchReturn;