Skip to content

Instantly share code, notes, and snippets.

@tjoskar
Last active March 13, 2017 18:00
Show Gist options
  • Save tjoskar/8d97d9c7b2ab593eba0580f226f13c97 to your computer and use it in GitHub Desktop.
Save tjoskar/8d97d9c7b2ab593eba0580f226f13c97 to your computer and use it in GitHub Desktop.
sw-sync.js
self.addEventListener('sync', event => {
console.log('sync event: ', event.tag);
// Misuse of sync tags. Should use a queue instead
if (event.tag.startsWith('news-article-')) {
const articleId = Number(event.tag.substr(13));
if (isNaN(articleId)) {
return true;
}
const updateAndShowNotification = async () => {
await updateCache(`https://happy-news-nmnepmqeqo.now.sh/${articleId}`);
await registration.showNotification('The article is ready for you', {
icon: 'icon.png',
body: 'View the article',
data: articleId
});
}
event.waitUntil(updateAndShowNotification());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment