Goals: Add links that are reasonable and good explanations of how stuff works. No hype and no vendor content if possible. Practical first-hand accounts of models in prod eagerly sought.
| /** | |
| * Can be defined as a config | |
| **/ | |
| const updateCount = 1000; | |
| const allowedRollForDbUpdate = 5 | |
| const updateViews = () => { | |
| const currentRoll = Math.round(Math.random()*updateCount); | |
| if(currentRoll === allowedRollForDbUpdate) { | |
| ...write 1000 views to DB |
| navigator.serviceWorker.getRegistrations().then(function (registrations) { | |
| for (let registration of registrations) { | |
| registration.unregister(); | |
| } | |
| }); |
| navigator.serviceWorker.getRegistrations().then(function(registrations) { | |
| for(let registration of registrations) { | |
| registration.unregister(); | |
| } }) |
| navigator.serviceWorker.getRegistrations().then(function(registrations) { | |
| for(let registration of registrations) { | |
| registration.unregister() | |
| } }) |
| async function updateCacheFromResponse(cache, request, fetchResponse) { | |
| if (fetchResponse.status >= 200 && fetchResponse.status < 300) { | |
| await cache.put(request, fetchResponse); | |
| } | |
| console.log("Cache updated"); | |
| } | |
| /** | |
| * Lazily updates cache in background from network | |
| * @param {*} request | |
| */ |
| /** | |
| * Activates the new service worker and clean old cache incase of version changes | |
| */ | |
| self.addEventListener("activate", (event) => { | |
| const currentCaches = [UNVARYING_CACHE, VOLATILE]; | |
| event.waitUntil( | |
| caches | |
| .keys() | |
| .then((cacheNames) => { | |
| return cacheNames.filter( |
| /** | |
| * We can precache the image urls, fonts as they don't update frequently | |
| */ | |
| self.addEventListener("install", (installEvent) => { | |
| installEvent.waitUntil( | |
| caches | |
| .open(UNVARYING_CACHE) | |
| .then((cache) => cache.addAll(CACHED_ONLY_URLS)) | |
| .then(() => { | |
| console.log( |
| if ("serviceWorker" in navigator) { | |
| window.addEventListener("load", function () { | |
| navigator.serviceWorker.register("/service-worker.js").then( | |
| function (registration) { | |
| // Registration was successful | |
| console.log( | |
| "ServiceWorker registration successful with scope: ", | |
| registration.scope | |
| ); | |
| }, |