Skip to content

fix(sw): return inner promise in activate cache cleanup#7928

Merged
evenstensberg merged 1 commit into
webpack:mainfrom
ryzrr:fix/service-worker-cache-cleanup
Mar 6, 2026
Merged

fix(sw): return inner promise in activate cache cleanup#7928
evenstensberg merged 1 commit into
webpack:mainfrom
ryzrr:fix/service-worker-cache-cleanup

Conversation

@ryzrr
Copy link
Copy Markdown
Member

@ryzrr ryzrr commented Mar 6, 2026

Summary
In the service worker's activate event handler, the inner cache.keys().then() promise was never returned from the outer .then() callback. this caused event.waitUntil() to resolve immediately with undefined before any stale cache entries were deleted. Old previous cache entries from previous deployment would be accumulated in users browsers with no guaranteed of cleanup. this fix returns the full promise chain and replaces the for loop with Promise.all() so all deletions run in parallel and event.waitUntil() only resolves once all stale entries are gone.

What kind of change does this PR introduce?
fix

Did you add tests for your changes?
No, the service worker runs in a browser context outside the Jest test environment.

Does this PR introduce a breaking change?
No

If relevant, what needs to be documented once your changes are merged or what have you already documented?
N/A

Use of AI
N/A

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.
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
webpack-js-org Ready Ready Preview, Comment Mar 6, 2026 2:45pm

Request Review

@alexander-akait
Copy link
Copy Markdown
Member

@evenstensberg Please take a look

@evenstensberg evenstensberg merged commit 2c4b6a6 into webpack:main Mar 6, 2026
9 checks passed
@alexander-akait
Copy link
Copy Markdown
Member

@ryzrr Do you have an issue about it?

@ryzrr
Copy link
Copy Markdown
Member Author

ryzrr commented Mar 7, 2026

@alexander-akait No existing issue for this one , I noticed it while going through the sw code and will be happy to open an issue first if you prefer that before raising a PR.

@alexander-akait
Copy link
Copy Markdown
Member

@ryzrr No need, just a question

Comment thread src/sw.js
caches.open(cacheName).then((cache) => {
caches.open(cacheName).then((cache) =>
// clean up those who are not listed in manifestURLs
cache.keys().then((keys) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, the inner Promise doesn't need to be returned. But indeed the outer Promise will resolve eagerly when the inner one hasn't finished resolving yet.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification ! My intention was to ensure that all cache delete operations are awaited so event wait until resolves only after the cleanup finishes.

pranjalisr pushed a commit to pranjalisr/webpack.js.org that referenced this pull request Mar 12, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants