/** * Caching strategy for handling requests. * - `cache-first`: Return from cache if fresh (within TTL), otherwise fetch from network immediately. Stale cache is only used when offline (network request fails). No background updates. * - `network-first`: Try network first, fall back to stale cache if offline (within stale TTL). Cache updated when network succeeds. * - `stale-while-revalidate`: Return from cache immediately (fresh = no update, stale = update in background). Fetch from network if too stale or missing. */ type CacheStrategy = "cache-first" | "network-first" | "stale-while-revalidate"; /** * Descriptor for matching incoming requests against user-configured request specs. * Used to identify requests by pathname and method (e.g. for cache clearing on logout). */ type RequestDescriptor = { /** * - URL or path (e.g. "/api/logout" or "https://example.com/api/logout") */ url: Request["url"]; /** * - HTTP method (e.g. "POST", "GET", "DELETE") */ method: Request["method"]; }; /** * Logging level for cache operations. * - `none`: No logging * - `minimal`: Logs cache hits and cache invalidation only * - `verbose`: Logs all events including cache misses, cleanup, and header usage */ type LoggingLevel = "none" | "minimal" | "verbose"; type HandleRequestConfig = { /** * - Name of the cache, used when calling `Cache.open(cacheName)` internally. Changing this name effectively clears the previous cache entries. */ cacheName: string; /** * - URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. Note: Cross-origin requests are never cached, regardless of scope or TTL headers. */ scope?: string[]; /** * - Default caching strategy: `'cache-first'`, `'network-first'`, or `'stale-while-revalidate'`. */ defaultStrategy?: CacheStrategy; /** * - Maximum age for fresh content. Fresh content will be returned from cache for cache-first and stale-while-revalidate strategies, and also from network-first when offline. Fresh content does not get updated from the network. Since this defaults to `300`, caching is automatic by default for GET requests matching the scope. Set to `0` or `undefined` to disable automatic caching (individual requests can still enable caching with `X-SW-Cache-TTL-Seconds` header). */ defaultTTLSeconds?: number; /** * - Maximum age for stale content. Stale content will be returned from cache for cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies. That means responses past the fresh TTL but within stale TTL can still be returned from cache. Stale content does get updated from the network. */ defaultStaleTTLSeconds?: number; /** * - Automatically invalidate cache on POST/PATCH/PUT/DELETE requests. */ inferInvalidation?: boolean; /** * - Custom fetch function to use for network requests. Receives a `Request` object and must return a `Promise`. Useful for handling authentication errors (401/403) or adding custom headers to all requests. */ customFetch?: typeof fetch; /** * - Maximum age (in seconds) before cache entries are automatically cleaned up. Entries older than this age are deleted. Defaults to 7200 seconds (2 hours, which is 2x the default stale TTL). Cache entries are cleaned up reactively (when accessed) and periodically (every 100 fetches). */ maxCacheAgeSeconds?: number; /** * - Logging level: "none" (no logging), "minimal" (cache hits and invalidation only), or "verbose" (all logging including misses, cleanup, and headers). Defaults to "none". */ loggingLevel?: LoggingLevel; /** * - List of request descriptors that trigger cache clearing when matched. Matches by pathname (ignores query) and method. Useful for logout endpoints (e.g. `[{ url: "/api/logout", method: "POST" }]`). */ clearCacheRequests?: RequestDescriptor[]; };