-
-
Save tjoskar/8d97d9c7b2ab593eba0580f226f13c97 to your computer and use it in GitHub Desktop.
sw-sync.js
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('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