Using multiple javascript service workers at the same domain but different folders

My website offers an array of web apps for each client. Each client has a different mix of apps that can be used.

Each web app is a hosted in a different folder.

So I need to cache for each client only it’s allowed web apps instead of caching all the apps, many of them he the user, will not use at all.

I naively created a global service worker for the shell of the website and custom named service worker for each folder or app.

However I noticed that after the first service worker, say sw_global.js service worker registers, installs, activates, fetches succesfully and creates a cache named cache-global, the second service worker, say sw_app1,js, which creates it’s own cache cache-app1, clears all cache-global.

How can I use custom service worker for each folder?. Please keep in mind that each folder is a microservice or a web app which is not necesarilly allowed for all users, so it’s imperative to not cache all content from a unique service worker.

Actually I code on vanilla.js, no angular, no react, no vue, no nada.

There’s two things to keep in mind here:

  1. It’s possible to register an arbitrary number of service workers for a given origin, as long as each service worker has its own unique scope. It sounds like you’re doing this already, registering both a top-level service worker with a scope of / and then additional service workers scoped to the paths from which each of your independent web apps run.

  2. The Cache Storage API (and other storage mechanisms, like IndexedDB) are shared throughout the entire origin, and by default there’s no “sharding” or namespace segregation.

Read More:   jss how to change opacity for a color

Taking those two facts together, my guess is that what’s happening is that you have some cache cleanup code in the activate handler of one of the service workers that’s scoped to a specific web app, and that code retrieves a list of current cache names and deletes any caches that it thinks is out of date. That’s a common thing to do in a service worker, but you can run into trouble if you don’t take into account the fact that caches.keys() will return a list of all the caches in your origin, even the caches that were created by other service worker instances.

Assuming that’s what’s going on here, a model that I’d recommend following is to include the value of registration.scope as part of the cache name when you create your caches. When it comes time to delete old cache entries in your activate handler, it’s easy to make sure that you’re only cleaning up caches that your specific service worker is supposed to manage by filtering out those that don’t match registration.scope.

E.g., something like this:

const CACHE_VERSION = 'v2';
const CACHE_NAME = `${registration.scope}!${CACHE_VERSION}`;

self.addEventListener('install', (event) => {
  event.waitUntil( => {
      // Add entries to the cache...

self.addEventListener('activate', (event) => {
  const cleanup = async () => {
    const cacheNames = await caches.keys();
    const cachesToDelete = cachesNames.filter(
        (cacheName) => cacheName.startsWith(`${registration.scope}!`) &&
                       cacheName !== CACHE_NAME);
    await Promise.all(
      (cacheName) => caches.delete(cacheName)));


I don’t know, but the accepted answer didn’t worked for me as it has typos as wells missing code I think. I am putting below what worked for me. Though I don’t want to take away any credit from the accepted answer, but just adding a working code, which will help others.

self.addEventListener('activate', (event) => {
  const cleanup = async () => {
    const cacheNames = await caches.keys();// here it should be caches
    const cachesToDelete = cacheNames.filter(      
    (cacheNames) => (cacheNames.startsWith(`${registration.scope}!`) && cacheNames !== CACHE_NAME));
   // await Promise.all(;// this line rewritten to make it working.
    await Promise.all(>{ 
      return caches.delete(CACHE_NAME);

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts