Skip to content

Commit 0cd87a9

Browse files
ryzrrpranjalisr
authored andcommitted
fix(sw): return inner promise in activate cache cleanup (webpack#7928)
The cache.keys().then() chain inside the activate event handler was not being returned, so event.waitUntil() resolved immediately with undefined before any stale cache entries were deleted. This meant old cache entries from previous deployments would accumulate in users' browsers silently with no guarantee of cleanup. Fix by returning the inner promise chain and using Promise.all() to delete stale entries in parallel, ensuring event.waitUntil() only resolves once all deletions are complete.
1 parent c4e38af commit 0cd87a9

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

src/sw.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ self.addEventListener("install", (event) => {
3838
self.addEventListener("activate", (event) => {
3939
// - [x] clean up outdated runtime cache
4040
event.waitUntil(
41-
caches.open(cacheName).then((cache) => {
41+
caches.open(cacheName).then((cache) =>
4242
// clean up those who are not listed in manifestURLs
43-
cache.keys().then((keys) => {
44-
for (const request of keys) {
45-
if (!manifestURLs.includes(request.url)) {
46-
cache.delete(request);
47-
}
48-
}
49-
});
50-
}),
43+
cache
44+
.keys()
45+
.then((keys) =>
46+
Promise.all(
47+
keys
48+
.filter((request) => !manifestURLs.includes(request.url))
49+
.map((request) => cache.delete(request)),
50+
),
51+
),
52+
),
5153
);
5254
});
5355

0 commit comments

Comments
 (0)