UNPKG

@ai-growth/nextjs

Version:

Seamlessly integrate Sanity CMS with Next.js applications for automated blog routing and rendering

150 lines 4.78 kB
/** * @fileoverview Enhanced CMS Content Hooks with Advanced Caching * * This module provides React hooks for fetching CMS content with comprehensive * caching strategies, SWR patterns, and cache invalidation capabilities. */ import { type ContentFetchOptions } from '../utils'; import type { CmsContent } from '../types'; /** * Enhanced cache options for CMS content */ export interface CmsContentCacheOptions extends ContentFetchOptions { /** Content type for caching */ contentType?: string; /** Initial data to prevent loading state */ initialData?: CmsContent | null; /** Whether fetching is enabled */ enabled?: boolean; /** Cache time to live (ms) */ ttl?: number; /** Stale time (ms) - data considered fresh within this time */ staleTime?: number; /** Whether to revalidate in background when stale */ revalidateInBackground?: boolean; /** Whether to refetch on window focus */ refetchOnWindowFocus?: boolean; /** Whether to refetch on reconnect */ refetchOnReconnect?: boolean; /** Success callback */ onSuccess?: (data: CmsContent) => void; /** Error callback */ onError?: (error: Error) => void; /** Custom cache tags for invalidation */ cacheTags?: string[]; } /** * Enhanced result for cached CMS content */ export interface CmsContentCachedResult { /** Content data */ content: CmsContent | null; /** Loading state */ isLoading: boolean; /** Error state */ isError: boolean; /** Error object */ error: Error | null; /** Currently fetching (may be background) */ isFetching: boolean; /** Data is stale but showing cached version */ isStale: boolean; /** Background revalidation in progress */ isRevalidating: boolean; /** Whether this was a cache hit */ cacheHit: boolean; /** Manual refetch function */ refetch: () => Promise<void>; /** Reset hook state */ reset: () => void; /** Invalidate cache for this content */ invalidate: () => Promise<void>; } /** * Content list cache options */ export interface CmsContentListCacheOptions { /** Content type */ contentType: string; /** List query options */ listOptions?: any; /** Whether fetching is enabled */ enabled?: boolean; /** Cache TTL */ ttl?: number; /** Stale time */ staleTime?: number; /** Revalidate in background */ revalidateInBackground?: boolean; /** Success callback */ onSuccess?: (data: CmsContent[]) => void; /** Error callback */ onError?: (error: Error) => void; } /** * Content list result */ export interface CmsContentListCachedResult { /** Content list */ content: CmsContent[]; /** Loading state */ isLoading: boolean; /** Error state */ isError: boolean; /** Error object */ error: Error | null; /** Fetching state */ isFetching: boolean; /** Stale state */ isStale: boolean; /** Revalidating state */ isRevalidating: boolean; /** Cache hit */ cacheHit: boolean; /** Has more items */ hasMore: boolean; /** Total count */ totalCount?: number; /** Refetch function */ refetch: () => Promise<void>; /** Load more function */ loadMore: () => Promise<void>; /** Reset function */ reset: () => void; /** Invalidate cache */ invalidate: () => Promise<void>; } /** * Enhanced hook for fetching CMS content by slug with advanced caching */ export declare function useCmsContentCached(slug: string | null, options?: CmsContentCacheOptions): CmsContentCachedResult; /** * Enhanced hook for fetching CMS content by ID with caching */ export declare function useCmsContentByIdCached(id: string | null, options?: Omit<CmsContentCacheOptions, 'contentType'>): CmsContentCachedResult; /** * Hook for cache invalidation operations */ export declare function useCacheInvalidation(): { invalidateContent: (contentType: string, slug?: string) => Promise<void>; invalidateContentLists: (contentType?: string) => Promise<void>; invalidateByTags: (tags: string[]) => Promise<void>; invalidateByPattern: (pattern: RegExp) => Promise<void>; clearAllCache: () => Promise<void>; }; /** * Hook for cache preloading */ export declare function useCachePreloading(): { preloadContent: (contentType: string, slug: string, fetchOptions?: ContentFetchOptions) => Promise<void>; preloadContentById: (id: string, fetchOptions?: ContentFetchOptions) => Promise<void>; }; /** * Hook for cache monitoring and debugging */ export declare function useCacheMonitoring(): { metrics: import("../utils").CacheMetrics; refreshMetrics: () => void; resetMetrics: () => void; }; //# sourceMappingURL=useCmsContentCached.d.ts.map