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

209
Vistas
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 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