Created
February 26, 2022 16:53
-
-
Save sloppylopez/e6a520f828a73f9525e6333ae770152d to your computer and use it in GitHub Desktop.
Service worker exampe
This file contains 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 characters
self.addEventListener('install', function(event) { | |
// event.waitUntil( | |
// caches.open('v1').then(function(cache) { | |
// return cache.addAll([ | |
// '/sw-test/', | |
// '/sw-test/index.html', | |
// '/sw-test/style.css', | |
// '/sw-test/app.js', | |
// '/sw-test/image-list.js', | |
// '/sw-test/star-wars-logo.jpg', | |
// '/sw-test/gallery/bountyHunters.jpg', | |
// '/sw-test/gallery/myLittleVader.jpg', | |
// '/sw-test/gallery/snowTroopers.jpg' | |
// ]); | |
// }) | |
// ); | |
}); | |
self.addEventListener('fetch', function(event) { | |
event.respondWith(caches.match(event.request).then(function(response) { | |
// caches.match() always resolves | |
// but in case of success response will have value | |
if (response !== undefined) { | |
return response; | |
} else { | |
return fetch(event.request).then(function (response) { | |
// response may be used only once | |
// we need to save clone to put one copy in cache | |
// and serve second one | |
let responseClone = response.clone(); | |
// caches.open('v1').then(function (cache) { | |
// cache.put(event.request, responseClone); | |
// }); | |
return response; | |
}).catch(function (e) { | |
console.log(e) | |
// return caches.match('/sw-test/gallery/myLittleVader.jpg'); | |
}); | |
} | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment