Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

331
Vistas
Can't notify users after updating service worker (workbox, cra)

I'm trying to let user know that the app is updated after installing the new service worker. But the problem is after doing the typical skipWaiting() and ClientsClaim() and trying to send message to clients that there is an update, clients array is empty.

index.js

serviceWorkerRegistration.Register();

serviceWorkerRegistration.js


function registerValidSW(swUrl, config) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing;
        if (installingWorker == null) {
          return;
        }
        installingWorker.onstatechange = () => {
          if (installingWorker.state === "installed") {
            if (navigator.serviceWorker.controller) {
              // here workbox sends message to service-worker.js
              registration.waiting.postMessage({ type: "SKIP_WAITING" });

              if (config && config.onUpdate) {
                config.onUpdate(registration);
              }
            } else {
              if (config && config.onSuccess) {
                config.onSuccess(registration);
              }
            }
          }
        };
      };
    })
    .catch(error => {
      console.error("Error during service worker registration:", error);
    });
}

service-worker.js

self.addEventListener("message", event => {
  if (event.data && event.data.type === "SKIP_WAITING") {
    skipWaiting();
    clientsClaim();
    //clients array is empty here
    self.clients.matchAll().then(clients => {
       clients.forEach(client => {
        client.postMessage("reload-window");
      });
    });
  }
});

Clients array is empty in service-worker.js, That being the case, I can't communicate with user.

(I've also setup notifications with this service worker, Clients array is not empty there as a result I've no problem communicating with user)

Any help is greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I was able to reproduce the problem.

Should've been doing this part:

self.clients.matchAll().then(clients => {
       clients.forEach(client => {
        client.postMessage("reload-window");
      });
    });

while listening to activate event like this:

self.addEventListener("activate", () => {
  self.clients.matchAll().then(clients => {
    clients.forEach(client => {
      client.postMessage("reload-window");
    });
  });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda