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

204
Visualizações
Why is this service worker logic not working?

I have the following service worker written in TypeScript:

self.addEventListener("fetch", (event: any) => {
    if (event.request.method == "POST") {
        event.respondWith(
            fetch(event.request).catch(error => {
                return caches.match(event.request);
            })
        );
    } else {
        event.respondWith(
            caches.match(event.request).then(cachedResponse => {
                // It can update the cache to serve updated content on the next request
                return cachedResponse || fetch(event.request);
            })
        );
    }
});

What I want it to do is if there's a POST request on the page, use a network first caching strategy (fetch from the network).

If there aren't any changes (not POST), use the cache first strategy (fetch from cache).

The issue is that it doesn't work. It will never fetch from the network. I have also tried to return on POST like this:

self.addEventListener("fetch", (event: any) => {
    if (event.request.method == "POST") return;
    event.respondWith(
        caches.match(event.request).then(cachedResponse => {
            // It can update the cache to serve updated content on the next request
            return cachedResponse || fetch(event.request);
        })
    );
});

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
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