Created
March 31, 2017 18:07
-
-
Save pemrouz/8cdf7d800ffe069ef35eaa9b9a736f9f to your computer and use it in GitHub Desktop.
simplifying sw api?
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 characters
// before (waitUntil) | |
self.addEventListener('install', function(event) { | |
event.waitUntil( | |
caches.open('simple-sw-v1').then(function(cache) { | |
return cache.addAll([ | |
'./', | |
'style.css', | |
'logging.js' | |
]) | |
}) | |
) | |
}) | |
// after (waitUntil) | |
self.addEventListener('install', caches | |
.open('simple-sw-v1') | |
.then(cache => cache.addAll([ | |
'./' | |
, 'style.css' | |
, 'logging.js' | |
]) | |
)) | |
// before (respondWith) | |
self.addEventListener('fetch', function(event) { | |
event.respondWith( | |
caches.match(event.request).then(function(response) { | |
return response || fetch(event.request) | |
}) | |
) | |
}) | |
// after (respondWith) | |
self.addEventListener('fetch', event => { | |
return caches | |
.match(event.request) | |
.then(response => response || fetch(event.request)) | |
}) | |
// note: it's not fundamentally not possible to use | |
// async/await with the waitUntil/respondWith approach | |
// after - async/await (waitUntil) | |
self.addEventListener('install', async event => { | |
const cache = await caches.open('simple-sw-v1') | |
return cache.addAll([ | |
'./' | |
, 'style.css' | |
, 'logging.js' | |
]) | |
}) | |
// after - async/await (respondWith) | |
self.addEventListener('fetch', async event => { | |
const response = await caches.match(event.request) | |
return response || fetch(event.request) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment