feat(pwa): Cache-Strategy-Entscheider + Unit-Tests
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:
hsiegeln
2026-04-18 16:32:30 +02:00
parent 02df0331b7
commit d38992661c
2 changed files with 83 additions and 0 deletions

View 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';
}

View File

@@ -0,0 +1,41 @@
import { describe, it, expect } from 'vitest';
import { resolveStrategy } from '../../src/lib/sw/cache-strategy';
describe('resolveStrategy', () => {
it('images bucket for /images/*', () => {
expect(resolveStrategy({ url: '/images/favicon-abc.png', method: 'GET' })).toBe('images');
});
it('swr for recipe HTML pages', () => {
expect(resolveStrategy({ url: '/recipes/42', method: 'GET' })).toBe('swr');
});
it('swr for recipe API reads', () => {
expect(resolveStrategy({ url: '/api/recipes/42', method: 'GET' })).toBe('swr');
expect(resolveStrategy({ url: '/api/recipes/all?sort=name', method: 'GET' })).toBe('swr');
expect(resolveStrategy({ url: '/api/wishlist', method: 'GET' })).toBe('swr');
});
it('network-only for write methods', () => {
expect(resolveStrategy({ url: '/api/recipes/42', method: 'PATCH' })).toBe('network-only');
expect(resolveStrategy({ url: '/api/recipes/42/favorite', method: 'PUT' })).toBe('network-only');
expect(resolveStrategy({ url: '/api/wishlist', method: 'POST' })).toBe('network-only');
});
it('network-only for online-only endpoints even on GET', () => {
expect(resolveStrategy({ url: '/api/recipes/import', method: 'GET' })).toBe('network-only');
expect(resolveStrategy({ url: '/api/recipes/preview?url=x', method: 'GET' })).toBe('network-only');
expect(resolveStrategy({ url: '/api/recipes/search/web?q=x', method: 'GET' })).toBe('network-only');
});
it('shell bucket for build/static assets', () => {
expect(resolveStrategy({ url: '/_app/immutable/chunks/x.js', method: 'GET' })).toBe('shell');
expect(resolveStrategy({ url: '/icon-192.png', method: 'GET' })).toBe('shell');
expect(resolveStrategy({ url: '/manifest.webmanifest', method: 'GET' })).toBe('shell');
});
it('falls through to swr for other same-origin GETs (e.g. root page)', () => {
expect(resolveStrategy({ url: '/', method: 'GET' })).toBe('swr');
expect(resolveStrategy({ url: '/wishlist', method: 'GET' })).toBe('swr');
});
});