fix: precache only stable shell paths so SW install survives a production build

This commit is contained in:
2026-07-20 14:31:26 +01:00
parent a78f8e9a3a
commit d0c9378c40
2 changed files with 12 additions and 2 deletions
+5 -1
View File
@@ -1,5 +1,9 @@
const CACHE = 'director-v1';
const SHELL = ['/', '/index.html', '/src/main.js', '/src/styles.css'];
// Precache only stable, build-invariant navigation paths. Hashed asset
// bundles (/assets/*) are cached at runtime on first online load by the
// cache-first fetch handler below. (Follow-up: generate a full precache
// list from the Vite build manifest for cold-offline-before-first-visit.)
const SHELL = ['/', '/index.html'];
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL)).then(() => self.skipWaiting()));
+7 -1
View File
@@ -18,9 +18,15 @@ describe('PWA wiring', () => {
expect(js).toContain('serviceWorker');
expect(js).toContain("register('/sw.js')");
});
it('service worker precaches the shell', () => {
it('service worker precaches only stable shell paths', () => {
const sw = readFileSync('public/sw.js', 'utf8');
expect(sw).toContain('addEventListener');
expect(sw).toContain('caches');
const shellMatch = sw.match(/SHELL\s*=\s*\[([^\]]*)\]/);
expect(shellMatch).not.toBe(null);
const shell = shellMatch[1];
expect(shell).toContain("'/'");
expect(shell).toContain("'/index.html'");
expect(shell).not.toContain('/src/');
});
});