Skip to content

Commit a101a4b

Browse files
committed
fix(sw): ship self-unregistering service worker
A previous deployment registered a serwist-based service worker that's still active in some browsers, intercepting requests and serving stale cached content. The codebase no longer registers a SW, but the old one keeps running until something replaces it. This file is what the browser fetches when checking for SW updates. On activation it clears all caches, unregisters itself, and reloads controlled pages so they refetch from the network.
1 parent 5b7929a commit a101a4b

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

public/serwist/sw.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Self-destructing service worker.
2+
// A previous deployment registered a serwist-based SW that's still active
3+
// in some user browsers, intercepting requests and serving stale cached
4+
// content. Replacing that SW with this one tells the browser to:
5+
// 1. Activate immediately (skipWaiting)
6+
// 2. Take control of open clients (clients.claim)
7+
// 3. Delete every cache it owns
8+
// 4. Unregister itself
9+
// 5. Reload all controlled pages so they fetch fresh from the network
10+
// After one visit, the SW is gone and behavior is normal forever after.
11+
12+
self.addEventListener("install", (event) => {
13+
event.waitUntil(self.skipWaiting());
14+
});
15+
16+
self.addEventListener("activate", (event) => {
17+
event.waitUntil(
18+
(async () => {
19+
const cacheNames = await caches.keys();
20+
await Promise.all(cacheNames.map((name) => caches.delete(name)));
21+
await self.clients.claim();
22+
const registration = self.registration;
23+
if (registration) await registration.unregister();
24+
const clients = await self.clients.matchAll({ type: "window" });
25+
for (const client of clients) {
26+
if ("navigate" in client) client.navigate(client.url);
27+
}
28+
})(),
29+
);
30+
});

0 commit comments

Comments
 (0)