2026-04-20 08:29:00 +02:00
|
|
|
export type CacheStrategy = 'shell' | 'network-first' | 'images' | 'network-only';
|
2026-04-18 16:32:30 +02:00
|
|
|
|
2026-04-18 22:14:38 +02:00
|
|
|
type RequestShape = { url: string; method: string };
|
2026-04-18 16:32:30 +02:00
|
|
|
|
|
|
|
|
// Pure function — sole decision-maker for "which strategy for this request?".
|
|
|
|
|
// Called by the service worker for every fetch event.
|
|
|
|
|
export function resolveStrategy(req: RequestShape): CacheStrategy {
|
|
|
|
|
// All write methods: never cache.
|
|
|
|
|
if (req.method !== 'GET' && req.method !== 'HEAD') return 'network-only';
|
|
|
|
|
|
|
|
|
|
// Reduce URL to pathname — query string not needed for matching
|
|
|
|
|
// except that online-only endpoints need no special handling here.
|
|
|
|
|
const path = req.url.startsWith('http') ? new URL(req.url).pathname : req.url.split('?')[0];
|
|
|
|
|
|
|
|
|
|
// Explicitly online-only GETs
|
|
|
|
|
if (
|
|
|
|
|
path === '/api/recipes/import' ||
|
|
|
|
|
path === '/api/recipes/preview' ||
|
|
|
|
|
path.startsWith('/api/recipes/search/web')
|
|
|
|
|
) {
|
|
|
|
|
return 'network-only';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Images
|
|
|
|
|
if (path.startsWith('/images/')) return 'images';
|
|
|
|
|
|
|
|
|
|
// App-shell: build assets and known static files
|
|
|
|
|
if (
|
|
|
|
|
path.startsWith('/_app/') ||
|
|
|
|
|
path === '/manifest.webmanifest' ||
|
|
|
|
|
path === '/icon.svg' ||
|
|
|
|
|
path === '/icon-192.png' ||
|
|
|
|
|
path === '/icon-512.png' ||
|
|
|
|
|
path === '/favicon.ico' ||
|
|
|
|
|
path === '/robots.txt'
|
|
|
|
|
) {
|
|
|
|
|
return 'shell';
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 08:29:00 +02:00
|
|
|
// Everything else: recipe pages, API reads, lists — network-first with
|
|
|
|
|
// timeout fallback to cache (handled in service-worker.ts).
|
|
|
|
|
return 'network-first';
|
2026-04-18 16:32:30 +02:00
|
|
|
}
|