-
-
Save bentzibentz/e5a939675b209e718eeef7f20627802c to your computer and use it in GitHub Desktop.
Revisions
-
ireade revised this gist
Dec 16, 2018 . 1 changed file with 15 additions and 13 deletions.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 @@ -1,19 +1,9 @@ self.addEventListener('install', (e) => { e.waitUntil( caches.open("precache").then((cache) => cache.add("/broken.png")) ); }); function isImage(fetchRequest) { return fetchRequest.method === "GET" && fetchRequest.destination === "image"; } @@ -24,8 +14,20 @@ self.addEventListener('fetch', (e) => { .then((response) => { if (response.ok) return response; // User is online, but response was not ok if (isImage(e.request)) { // Get broken image placeholder from cache return caches.match("/broken.png"); } }) .catch((err) => { // User is probably offline if (isImage(e.request)) { // Get broken image placeholder from cache return caches.match("/broken.png"); } }) // end fetch ) }); -
ireade created this gist
Dec 16, 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,31 @@ const precacheName = 'precache'; const precacheFiles = [ "/broken.png" ]; self.addEventListener('install', (e) => { self.skipWaiting(); e.waitUntil( caches.open(precacheName).then((cache) => cache.addAll(precacheFiles)) ); }); self.addEventListener('activate', (e) => { console.log('[ServiceWorker] Activated'); }); function isImage(fetchRequest) { return fetchRequest.method === "GET" && fetchRequest.destination === "image"; } self.addEventListener('fetch', (e) => { e.respondWith( fetch(e.request) .then((response) => { if (response.ok) return response; const isImage = e.request.method === "GET" && e.request.destination === "image"; if (isImage) return caches.match("/broken.png"); }) ) });