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

150
Vistas
Service worker - block fetch requests on initial app load only

Background - I have a firebase web app that loads and runs perfectly offline (i.e aeroplane mode) and online with a good connection. The problem is very weak connections where navigator.onLine is true, but the connection is basically unusable. The app hangs until I turn aircraft mode on. According to the Firebase SDK docs there doesn't seem to be a method to initializeApp() offline. I have Firestore offline persistence enabled and it works great once the app is loaded.

Goal - use the service worker to block fetch network requests for about 2 seconds by returning an error to 'trick' Firebase into thinking the device is offline and load instantly, then unblock/allow all future fetch requests as normal

The problem - the code I have found and implemented below works great initially and the app now loads instantly in all network conditions, but as Firebase tries again every 10 seconds or so to make fetch requests, the service worker continuous to intercept them which I don't want.

How do I make the following code run once only in a service worker on the initial load?

addEventListener("fetch", (event) => {
  event.respondWith(
    (async function () {
      console.log("blocking");
      event.request.url.startsWith("https://") ? Response.error() : fetch(event.request);
      return setTimeout(() => {
        console.log("unblocked");
        fetch(event.request);
      }, 3000);
    })()
  );
});
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