Helpers that make working with service workers a little easier.
npm install swkit or yarn add swkit
import{on,cacheAll,createRouter,networkFirst}from'swkit';constrouter=createRouter();// Create middleware that updates `precache` and falls back to only cache// in the absence of a network connection.constprecacheNetworkFirst=networkFirst('precache');router.get('/',precacheNetworkFirst);router.get('/style.css',precacheNetworkFirst);router.get('/randomnumber',request=>{returnnewResponse(Math.random().toString().substr(2));});router.get('/asyncrandomnumber',request=>{returnnewPromise((resolve,reject)=>{resolve(newResponse(Math.random().toString().substr(2)));});});// Middleware takes a Request object, and a parameters object.// params holds the values of the replaced url tokens (in this case, `id`).router.get('/items/:id',(req,params)=>{// Router expects the middleware to return a Promise or a ResponsereturnnewResponse('This is item '+params.id);});on('fetch',router.dispatch);on('install',e=>{e.waitUntil(cacheAll('precache',['/','/style.css']).then(skipWaiting()));});on('activate',e=>{e.waitUntil(clients.claim());});dist/index.js is in UMD format so it can be imported by a build process, or simply used via importScripts. If imported with importScripts, the contents of the library will be found under the global object swkit.