Compare commits

...

2 Commits

Author SHA1 Message Date
Codex Agent
4b1785fb85 Update beads tracker
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
2026-01-30 13:01:29 +01:00
Codex Agent
8aba034344 Respect cache-control in guest API cache 2026-01-30 13:00:19 +01:00
4 changed files with 49 additions and 1 deletions

View File

@@ -1 +1 @@
fotospiel-app-vc3
fotospiel-app-cbnv

View File

@@ -7,6 +7,7 @@ import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies';
import { shouldCacheResponse } from './lib/cachePolicy';
declare const self: ServiceWorkerGlobalScope & {
__WB_MANIFEST: Array<any>;
@@ -63,6 +64,9 @@ registerRoute(
cacheName: 'guest-api',
networkTimeoutSeconds: 6,
plugins: [
{
cacheWillUpdate: async ({ response }) => (shouldCacheResponse(response) ? response : null),
},
new CacheableResponsePlugin({ statuses: [0, 200] }),
new ExpirationPlugin({ maxEntries: 80, maxAgeSeconds: 60 * 60 * 24 }),
],

View File

@@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest';
import { shouldCacheResponse } from '../cachePolicy';
describe('shouldCacheResponse', () => {
it('returns false when Cache-Control is no-store', () => {
const response = new Response('ok', { headers: { 'Cache-Control': 'no-store' } });
expect(shouldCacheResponse(response)).toBe(false);
});
it('returns false when Cache-Control is private', () => {
const response = new Response('ok', { headers: { 'Cache-Control': 'private, max-age=0' } });
expect(shouldCacheResponse(response)).toBe(false);
});
it('returns false when Pragma is no-cache', () => {
const response = new Response('ok', { headers: { Pragma: 'no-cache' } });
expect(shouldCacheResponse(response)).toBe(false);
});
it('returns true for cacheable responses', () => {
const response = new Response('ok', { headers: { 'Cache-Control': 'public, max-age=60' } });
expect(shouldCacheResponse(response)).toBe(true);
});
});

View File

@@ -0,0 +1,20 @@
export function shouldCacheResponse(response: Response | null): boolean {
if (!response) {
return false;
}
const cacheControl = response.headers.get('Cache-Control') ?? '';
const pragma = response.headers.get('Pragma') ?? '';
const normalizedCacheControl = cacheControl.toLowerCase();
const normalizedPragma = pragma.toLowerCase();
if (normalizedCacheControl.includes('no-store') || normalizedCacheControl.includes('private')) {
return false;
}
if (normalizedPragma.includes('no-cache')) {
return false;
}
return true;
}