importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'); if (workbox) { workbox.core.skipWaiting(); workbox.core.clientsClaim(); workbox.routing.registerRoute(/wp-admin(.*)|wp-json(.*)|(.*)preview=true(.*)/, new workbox.strategies.NetworkOnly()); workbox.routing.registerRoute(({event}) => event.request.destination === 'document', async (args) => { try { const response = await new workbox.strategies.StaleWhileRevalidate({ cacheName: 'html', plugins: [ new workbox.expiration.ExpirationPlugin({ maxEntries: 50, }), new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [0, 200] }), ], }).handle(args); return response || await caches.match(''); } catch (error) { console.log('catch:', error); return await caches.match(''); } } ); workbox.routing.registerRoute(({event}) => event.request.destination === 'script', new workbox.strategies.StaleWhileRevalidate({ cacheName: 'javascript', plugins: [ new workbox.expiration.ExpirationPlugin({ maxEntries: 30, }), new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [0, 200] }), ], }) ); workbox.routing.registerRoute(({event}) => event.request.destination === 'style', new workbox.strategies.StaleWhileRevalidate({ cacheName: 'stylesheets', plugins: [ new workbox.expiration.ExpirationPlugin({ maxEntries: 30, }), new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [0, 200] }), ], }) ); workbox.routing.registerRoute(({event}) => event.request.destination === 'image', new workbox.strategies.StaleWhileRevalidate({ cacheName: 'images', plugins: [ new workbox.expiration.ExpirationPlugin({ maxEntries: 30, }), new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [0, 200] }), ], }) ); workbox.routing.registerRoute(({event}) => event.request.destination === 'font', new workbox.strategies.StaleWhileRevalidate({ cacheName: 'fonts', plugins: [ new workbox.expiration.ExpirationPlugin({ maxEntries: 30, }), new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [0, 200] }), ], }) ); } self.addEventListener('push', event => { if (event.data) { const pushData = event.data.json(); event.waitUntil( self.registration.showNotification(pushData.title, pushData) ); console.log(pushData); } else { console.log('No push data fetched'); } }); self.addEventListener('notificationclick', event => { event.notification.close(); if (event.action === 'action1') { event.waitUntil( clients.openWindow(event.notification.data.pushActionbutton1Url) ); } else if (event.action === 'action2') { event.waitUntil( clients.openWindow(event.notification.data.pushActionbutton2Url) ); } else { event.waitUntil( clients.openWindow(event.notification.data.url) ); } });