/** * @file Service Worker Registration * @description Comprehensive service worker lifecycle management with update * detection, offline/online events, and user notification support. */ /** * Service worker registration configuration */ export interface ServiceWorkerConfig { /** Path to service worker file */ swPath?: string; /** Called when SW is successfully installed for first time */ onSuccess?: (registration: ServiceWorkerRegistration) => void; /** Called when new SW content is available */ onUpdate?: (registration: ServiceWorkerRegistration) => void; /** Called when app goes offline */ onOffline?: () => void; /** Called when app comes back online */ onOnline?: () => void; /** Called on registration error */ onError?: (error: Error) => void; /** Called during SW registration */ onRegistering?: () => void; /** Enable registration in development (default: false) */ enableInDev?: boolean; /** Update check interval in ms (default: 60000 = 1 minute) */ updateCheckInterval?: number; } /** * Service worker state */ export interface ServiceWorkerState { /** Whether SW is supported */ isSupported: boolean; /** Whether SW is registered */ isRegistered: boolean; /** Whether a new version is available */ hasUpdate: boolean; /** Whether app is online */ isOnline: boolean; /** Current registration */ registration: ServiceWorkerRegistration | null; /** Any registration error */ error: Error | null; } /** * Service worker controller interface */ export interface ServiceWorkerController { /** Current state */ state: ServiceWorkerState; /** Update the service worker */ update: () => Promise; /** Skip waiting and activate new SW */ skipWaiting: () => void; /** Unregister the service worker */ unregister: () => Promise; /** Get SW version */ getVersion: () => Promise; /** Cache specific URLs */ cacheUrls: (urls: string[]) => void; /** Clear all caches */ clearCaches: () => void; /** Add state change listener */ subscribe: (listener: (state: ServiceWorkerState) => void) => () => void; } /** * Register service worker with lifecycle management * * @example * ```tsx * // In your app entry point * registerServiceWorker({ * onSuccess: () => console.log('App cached for offline'), * onUpdate: () => { * if (confirm('New version available! Reload?')) { * window.location.reload(); * } * }, * onOffline: () => toast.info('You are offline'), * onOnline: () => toast.success('Back online'), * }); * ``` */ export declare function registerServiceWorker(config?: ServiceWorkerConfig): ServiceWorkerController; /** * React hook for service worker state * * @example * ```tsx * function App() { * const { hasUpdate, isOnline, skipWaiting } = useServiceWorker(); * * return ( * <> * {hasUpdate && ( * * Update available! * * * )} * {!isOnline && You are offline} * * ); * } * ``` */ export declare function useServiceWorker(controller?: ServiceWorkerController): { isSupported: boolean; isRegistered: boolean; hasUpdate: boolean; isOnline: boolean; registration: ServiceWorkerRegistration | null; error: Error | null; update: () => Promise; skipWaiting: () => void; unregister: () => Promise; getVersion: () => Promise; cacheUrls: (urls: string[]) => void; clearCaches: () => void; }; /** * Unregister all service workers * * @example * ```tsx * // Useful for debugging or resetting app state * await unregisterServiceWorker(); * ``` */ export declare function unregisterServiceWorker(): Promise; /** * Check if service worker is controlling the page */ export declare function isServiceWorkerControlling(): boolean; /** * Wait for service worker to be ready */ export declare function waitForServiceWorker(): Promise; /** * Send message to service worker */ export declare function postMessageToSW(message: unknown): void;