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

201
Visualizações
Is there a way for SharedWorker to tell if it is about to get closed?

Is there a way to tell that the SharedWorker is about to get closed, something like the beforeunload event that window has?

I need this information inside of the worker to signal server of closing of web socket etc.

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

0

No.

But SharedWorker will be alive as long as there is any live page holding a reference to it (through new SharedWorker()). So what you can do is to post a message to your SharedWorker when your page is about to go away (pagehide event), and your SharedWorker can keep track of how many pages are alive and close the WebSocket when the live count drops to zero.

However, do watch out for the definition of "live page". Chrome (and other browsers as well) tries very hard to optimize the browser performance and energy impact. Chrome has this Tab Discarding feature where it will discard background pages when there is pressure on the memory. Discarding a page will throw away the page data without closing the window, so that when user clicks the windows again Chrome can reload the page. This usually isn't an issue for simple website, but given that you are running websocket inside SharedWorker this could have some implications on your overall protocol design. In particular, if all of your pages are discarded, your SharedWorker dies as well. And discarding a page has absolutely zero notification.

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