Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
How can I detect if a serviceworker install fails?

My serviceworker is partially generated by gulp, so there's some chance it ends up with a SyntaxError or TypeError if I've made a mistake, and I'd like to be about to detect in my navigator.serviceWorker.register code that this has occurred, and send an error report.

I haven't figured out how to do this. I figured there'd be some sort of "on fail" event but I haven't found one. It looks like maybe I'm looking for an SW with a redundant state...

I tried

const newWorker = reg.installing
newWorker.addEventListener('statechange', () => {
  if (newWorker.state == 'redundant') {
    // log error
  }
})

but the condition doesn't seem to fire. This condition also doesn't fire:

reg.addEventListener('updatefound', () => {
  if (reg.redundant) {
    // log error
  }
})

I don't see any examples in the SW tutorials of this, which is surprising since it seems like a sort of basic thing to want to notice and detect.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If there's a syntax error, or if your service worker contains anything that is invalid JavaScript, your call to navigator.serviceWorker.register() will reject. (It might also reject if there's a network error preventing the script from being downloaded.)

If your service worker contains valid JavaScript but throws a runtime exception prior to finishing the install step, then the service worker will transition from the installing state to redundant. Note that in this scenario, navigator.serviceWorker.register() will not reject, as the redundant state transition happens after the registration has succeeded.

navigator.serviceWorker.register('sw.js')
  .then((reg) => {
    // reg.installing may or may not be set, depending on whether
    // a new SW was registered.
    reg.installing?.addEventListener('statechange', (event) => {\
      if (event.target.state === 'redundant') {
        // There was a runtime error.
      }
    });
  })
  .catch((err) => console.log('SW registration failed:', err));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda