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

261
Vistas
trabajador del servicio de almacenamiento en caché rompiendo google maps api

Tengo problemas para que funcione mi aplicación web progresiva. Necesito evitar que almacene en caché la API de Google Maps. Intenté hacerlo para que no almacene en caché nada fuera del dominio, pero parece que no puedo resolverlo.

 cacheId = cacheIdRelace; staticCache = "static-" + cacheId; dynamicCache = "dynamic-" + cacheId; self.addEventListener('install', function(event) { console.log('[Service Worker] Installing Service Worker ...', event); self.skipWaiting(); event.waitUntil( caches.open(staticCache).then(function(cache) { cache.addAll(['/', '/index.html','offline/index.html', '/manifest.json', '/style/main.css']); }) ); }); self.addEventListener('activate', evt => { console.log('[Service Worker] Activating Service Worker ....'); evt.waitUntil( caches.keys().then (keys =>{ //console.log(keys); return Promise.all(keys .filter(key => key !== staticCache && key !== dynamicCache) .map(key => caches.delete(key)) ) }) ) }); self.addEventListener('fetch', evt => { evt.respondWith( caches.match(evt.request).then(cacheRes => { return cacheRes || fetch(evt.request).then(fetchRes => { return caches.open(dynamicCache).then(cache => { if ((evt.request.url).includes(window.location.host)) { cache.put(evt.request.url, fetchRes.clone()); } fetchRes.clone() return fetchRes }) }); }).catch(() => caches.match('/offline/')) ); });

Agregué la siguiente declaración para tratar de evitar que almacene en caché cualquier cosa fuera del dominio local, pero hice algo mal y solo arroja errores de búsqueda.

 if ((evt.request.url).includes(window.location.host)) {
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Debe verificar la URL y buscarla en el servidor si es la API de Google:

 self.addEventListener('fetch', function (event) { // no caching google api if (event.request.url.indexOf('maps.googleapis.com/maps/api/') > -1) { event.respondWith( fetch(event.request).then( function (response) { // Check if we received a valid response if (!response) { console.error("fetch eventhandler error: no response"); return Response.error(); } if (!response.ok) { console.warn("fetch eventhandler warning: status=", response.status, response.type); return response; } // ok return response; }).catch(function () { // failed access console.error("fetch eventhandler error"); return Response.error(); })) } else { event.respondWith( caches.match(event.request).then(cacheRes => { return cacheRes || fetch(event.request).then(fetchRes => { return caches.open(dynamicCache).then(cache => { if ((event.request.url).includes(window.location.host)) { cache.put(event.request.url, fetchRes.clone()); } fetchRes.clone() return fetchRes }) }); }).catch(() => caches.match('/offline/')) ); } });
about 4 years ago · Juan Pablo Isaza Denunciar

0

El código @nechoj sniped funciona perfectamente. Solo lo modifiqué un poco para almacenar en caché solo los activos que están en el mismo dominio.

 self.addEventListener('fetch', function (event) { // no caching google api if (!(event.request.url.indexOf(self.location.host) > -1)) { event.respondWith( fetch(event.request).then( function (response) { // Check if we received a valid response if (!response) { console.error("fetch eventhandler error: no response"); return Response.error(); } if (!response.ok) { console.warn("fetch eventhandler warning: status=", response.status, response.type); return response; } // ok return response; }).catch(function () { // failed access console.error("fetch eventhandler error"); return Response.error(); })) } else { event.respondWith( caches.match(event.request).then(cacheRes => { return cacheRes || fetch(event.request).then(fetchRes => { return caches.open(dynamicCache).then(cache => { cache.put(event.request.url, fetchRes.clone()); return fetchRes }) }); }).catch(() => caches.match('/offline/')) ); } });
about 4 years ago · Juan Pablo Isaza Denunciar
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