Files
MealQuest/web/src/sw/apiStrategy.ts
T

17 lines
776 B
TypeScript
Raw Normal View History

2026-10-05 15:22:37 -04:00
// The network-first strategy for API reads, kept apart from src/sw.ts so the
// tests can run it without a service worker.
import type { WorkboxPlugin } from 'workbox-core/types'
import { NetworkFirst } from 'workbox-strategies'
import { cacheNames, markCacheFallback, networkTimeoutSeconds } from './routes'
// NetworkFirst reads its cache only when the network fails or times out, so
// every cached response it uses is a fallback.
const markFallback: WorkboxPlugin = {
cachedResponseWillBeUsed: async ({ cachedResponse }) =>
cachedResponse === undefined ? undefined : markCacheFallback(cachedResponse),
}
export function createApiStrategy(): NetworkFirst {
return new NetworkFirst({ cacheName: cacheNames.api, networkTimeoutSeconds, plugins: [markFallback] })
}