Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active October 22, 2015 15:01
Show Gist options
  • Save jakearchibald/a88db50b629677c00c1f to your computer and use it in GitHub Desktop.
Save jakearchibald/a88db50b629677c00c1f to your computer and use it in GitHub Desktop.
onPageUnableToLoad(url => {
tellUserItsBeingFetchedInBackground();
navigator.serviceWorker.ready.then(reg => {
reg.sync.register({ tag: 'fetch-n-cache-' + url });
});
});
// 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