Skip to content

Commit 418a476

Browse files
committed
fix: Self-destroying SW to clear cached redirects
1 parent b8d822d commit 418a476

1 file changed

Lines changed: 12 additions & 49 deletions

File tree

website/public/sw.js

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,20 @@
1-
const CACHE_NAME = 'trinity-v2';
2-
const BASE_PATH = '/trinity';
3-
const STATIC_ASSETS = [
4-
BASE_PATH + '/',
5-
BASE_PATH + '/index.html',
6-
BASE_PATH + '/vite.svg',
7-
BASE_PATH + '/manifest.json'
8-
];
9-
10-
// Install - cache static assets
11-
self.addEventListener('install', (event) => {
12-
event.waitUntil(
13-
caches.open(CACHE_NAME).then((cache) => {
14-
return cache.addAll(STATIC_ASSETS);
15-
})
16-
);
1+
// Self-destroying service worker - unregisters itself and clears all caches
2+
self.addEventListener('install', () => {
173
self.skipWaiting();
184
});
195

20-
// Activate - clean old caches
216
self.addEventListener('activate', (event) => {
227
event.waitUntil(
23-
caches.keys().then((keys) => {
24-
return Promise.all(
25-
keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))
26-
);
27-
})
28-
);
29-
self.clients.claim();
30-
});
31-
32-
// Fetch - network first, fallback to cache
33-
self.addEventListener('fetch', (event) => {
34-
// Skip non-GET requests
35-
if (event.request.method !== 'GET') return;
36-
37-
// Skip external requests
38-
if (!event.request.url.startsWith(self.location.origin)) return;
39-
40-
event.respondWith(
41-
fetch(event.request)
42-
.then((response) => {
43-
// Clone and cache successful responses
44-
if (response && response.status === 200 && response.type === 'basic') {
45-
const clone = response.clone();
46-
caches.open(CACHE_NAME).then((cache) => {
47-
cache.put(event.request, clone);
48-
});
49-
}
50-
return response;
51-
})
52-
.catch(() => {
53-
// Fallback to cache
54-
return caches.match(event.request);
8+
Promise.all([
9+
// Unregister this service worker
10+
self.registration.unregister(),
11+
// Clear all caches
12+
caches.keys().then((keys) => {
13+
return Promise.all(keys.map((key) => caches.delete(key)));
5514
})
15+
])
5616
);
5717
});
18+
19+
// Don't intercept any requests
20+
self.addEventListener('fetch', () => {});

0 commit comments

Comments
 (0)