Dev.Lib
arrow_backBack to Blogs
LEVEL_066.6June 27, 20261 min read

Service Worker lifecycle traps

Web Security

New SW waits in `waiting` until tabs close — users stuck on old cache.

Summary

New SW waits in waiting until tabs close — users stuck on old cache. skipWaiting + clients.claim() forces update but can mid-session break. Version caches explicitly; delete old caches on activate. Scope limited to path — wrong registration path = no control. Auth tokens in SW cache = risk.

Code example

self.addEventListener('install', (event) => {
  self.skipWaiting();
});

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) =>
      Promise.all(keys.filter((k) => k !== CACHE_VERSION).map((k) => caches.delete(k)))
    )
  );
  clients.claim();
});

Key points

  • New SW waits in waiting until tabs close — users stuck on old cache.
  • skipWaiting + clients.claim() forces update but can mid-session break.
  • Version caches explicitly; delete old caches on activate.
  • Scope limited to path — wrong registration path = no control.
  • Auth tokens in SW cache = risk.

Further reading

Related concepts