-
-
Save jakearchibald/a88db50b629677c00c1f to your computer and use it in GitHub Desktop.
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
onPageUnableToLoad(url => { | |
tellUserItsBeingFetchedInBackground(); | |
navigator.serviceWorker.ready.then(reg => { | |
reg.sync.register({ tag: 'fetch-n-cache-' + url }); | |
}); | |
}); |
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
// serviceWorker | |
self.addEventListener('sync', event => { | |
if (event.registration.tag.startsWith('fetch-n-cache-')) { | |
const urlToFetch = event.registration.tag.slice('fetch-n-cache-'.length); | |
event.waitUntil( | |
caches.open('cached-pages') | |
.then(c => c.match(urlToFetch)) | |
.then(response => { | |
if (response) return; | |
return c.add(urlToFetch).then(_ => showNotification()) | |
}) | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment