Experimentar con Workbox
Clase 12 de 16 • Curso de Progressive Web Apps con React.js
Contenido del curso
Clase 12 de 16 • Curso de Progressive Web Apps con React.js
Contenido del curso
Hector Jose Flores Colmenarez
Bernardo Aguayo Ortega
lucasmoyano.ar Moyano
lucasmoyano.ar Moyano
Yasniel Fajardo Egues
Javier Alejandro Albornoz Pérez
Eduardo Rasgado Ruiz
Diego Forero
Carlos Loaiza
Omar García Betanzos
Pablo Nicolás Alonso
Brian Bentancourt
Fabián andres Pedraza Borhorquez
Naldo Duran
Mi solucion, espero les sea de ayuda.
workbox.routing.registerRoute( /^https?:\/\/www.themealdb.com\/images\/.*/, new workbox.strategies.CacheFirst({ cacheName: 'image-cache', plugins: [ new workbox.expiration.Plugin({ maxAgeSeconds: 7 * 24 * 60 * 60, maxEntries: 20 }) ] }) )
omg, hermoso
Mmm y si tengo que crear, modificar y eliminar entidades estando offline y después persistir cuando esté online... ¿Cuál sería la mejor estrategia?
Me autorespondo jeje... Estuve investigando mucho acerca de este tema... Se me hizo bastante complejo, pero encontré una solución bastante simple. Actualmente lo estoy solucionando de la siguiente forma... En las entidades que quiero que funcionen offline, en vez de utilizar un id autoincrement en la base de datos, utilizo UUID. De esta forma puedo crear los ids desde el frontend sin problemas. Después, a la hora de hacer un post requests, voy almacenando todas las request en un array en LocalStorage... devolviendo una respuesta mockeada... y cuando vuelve la conexión a internet ejecuto todos las request en el orden en que fueron almacenadas... La verdad esto me esta funcionando muy bien y me esta cubriendo casi todos los casos.
Mi solucion:
import { clientsClaim } from 'workbox-core'; import { ExpirationPlugin } from 'workbox-expiration'; import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; import { registerRoute } from 'workbox-routing'; import { NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'; const config = { maxEntries: 5000, maxAgeSeconds: 60 * 60 * 24 * 30, }; clientsClaim(); precacheAndRoute(self.__WB_MANIFEST); const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); registerRoute(({ request, url }) => { if (request.mode !== 'navigate') { return false; } if (url.pathname.startsWith('/_')) { return false; } if (url.pathname.match(fileExtensionRegexp)) { return false; } return true; }, createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`)); // Images registerRoute( ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), new StaleWhileRevalidate({ cacheName: 'images', plugins: [new ExpirationPlugin(config)], }), ); registerRoute( ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.svg'), new StaleWhileRevalidate({ cacheName: 'images', plugins: [new ExpirationPlugin(config)], }), ); registerRoute( ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.jpg'), new StaleWhileRevalidate({ cacheName: 'images', plugins: [new ExpirationPlugin(config)], }), ); // API registerRoute( /^https?:\/\/training-dev-api.lexawise.com\/zenith\/.*/, new StaleWhileRevalidate({ cacheName: 'all-https', plugins: [new ExpirationPlugin(config)], }), 'GET', ); // Default registerRoute( /^https?.*/, new NetworkFirst({ cacheName: 'all-https', plugins: [new ExpirationPlugin(config)], }), 'GET', ); registerRoute( /^http?.*/, new NetworkFirst({ cacheName: 'all-http', plugins: [new ExpirationPlugin(config)], }), 'GET', ); self.addEventListener('message', (event) => { if (event.data && event.data.type === 'SKIP_WAITING') { self.skipWaiting(); } });
Podrías pasar el repo?
Con la nueva ley europea no hay un aviso especial para nuestros usuarios para decirles que les estamos trackeando con service workers y google analytics? O va implicito en el aviso de las coockies?
Tienes que informar que datos recolectas ademas dar la opción de que borren toda su información del sistema.
Excelente curso!, muchas gracias
Documentación
Enlace a la documentación: https://developers.google.com/web/tools/workbox/modules/workbox-expiration
hola, en mi aplicacion react estoy uttilizando leaflet y hace llamadas http, por lo que workbox me da errores y no carga las urls. Como puedo solucionarlo?
workbox.routing.registerRoute( /(.*)articles(.*)\.(?:png|gif|jpg)/, workbox.strategies.cacheFirst({ cacheName: 'images-cache', plugins: [ new workbox.expiration.Plugin({ maxEntries: 50, maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days }) ] }) ); esta informacion la encuentras en la documentacion de wokbox
b