Created
June 28, 2018 20:39
-
-
Save ashhitch/8e2743c1b33e33f3a5d0ff68f9ef5ccf to your computer and use it in GitHub Desktop.
Revisions
-
ashhitch created this gist
Jun 28, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,113 @@ importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.3.0/workbox-sw.js'); //{clientsClaim: true} //console.log('this is my custom service worker'); const revision = 'version-1'; const longerRevision = 'static-1'; //Config workbox.setConfig({ debug: true }); // Offline Google Analytics workbox.googleAnalytics.initialize(); // Google fonts workbox.routing.registerRoute( new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'), workbox.strategies.cacheFirst({ cacheName: 'googleapis', revision: longerRevision, plugins: [ new workbox.expiration.Plugin({ maxEntries: 30, }), ], }), ); //Static routes workbox.routing.registerRoute( '/', workbox.strategies.networkFirst({ cacheName: 'network-first', revision: revision, plugins: [ new workbox.expiration.Plugin({ // Cache for a maximum of a week maxAgeSeconds: 7 * 24 * 60 * 60, }) ], }) ); workbox.routing.registerRoute( /\.(?:png|gif|jpg|jpeg|svg)$/, workbox.strategies.cacheFirst({ cacheName: 'images', revision: revision, plugins: [ new workbox.expiration.Plugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days }), ], }), ); workbox.routing.registerRoute( /.*(vendors|bundle).(css|js).*$/, workbox.strategies.staleWhileRevalidate({ cacheName: 'static-resources', revision: revision, plugins: [ new workbox.expiration.Plugin({ // Cache only 20 images maxEntries: 20, // Cache for a maximum of a week maxAgeSeconds: 7 * 24 * 60 * 60, }) ], }) ); workbox.routing.registerRoute( '/offline', workbox.strategies.cacheFirst({ cacheName: 'offline-page', revision: revision, }) ); const pageHandler = workbox.strategies.networkFirst({ cacheName: 'dynamic', networkTimetoutSeconds: 5, plugins: [ new workbox.expiration.Plugin({ maxEntries: 50, }) ] }); workbox.routing.registerRoute(function (routeData) { return routeData.event.request.headers.get('accept').includes('text/html'); }, args => { return pageHandler.handle(args).then(response => { if (!response) { return caches.match('/offline'); } // else if (response.status === 404) { // return caches.match('pages/404.html'); // } return response; }); }); //other stuff workbox.precaching.precacheAndRoute([]);