feat(pwa): Cache-Strategy-Entscheider + Unit-Tests
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m18s
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m18s
Reine TS-Funktion resolveStrategy({url, method}) → 'shell' | 'swr'
| 'images' | 'network-only'. Kernregel:
- Schreib-Methoden + import/preview/search/web → network-only
- /images/* → images (cache-first)
- /_app/, manifest, Icons, favicon, robots → shell (cache-first)
- Alles andere same-origin-GET → swr
7 Tests decken alle Buckets ab. Wird vom SW in Task 8 aufgerufen.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
42
src/lib/sw/cache-strategy.ts
Normal file
42
src/lib/sw/cache-strategy.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
export type CacheStrategy = 'shell' | 'swr' | 'images' | 'network-only';
|
||||
|
||||
export type RequestShape = { url: string; method: string };
|
||||
|
||||
// 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';
|
||||
}
|
||||
|
||||
// Everything else: recipe pages, API reads, lists — all SWR.
|
||||
return 'swr';
|
||||
}
|
||||
Reference in New Issue
Block a user