Skip to content

Instantly share code, notes, and snippets.

@robbiejaeger
Created July 12, 2017 00:19
Show Gist options
  • Save robbiejaeger/72cffc1f4770a9cb31e26da98d6001fe to your computer and use it in GitHub Desktop.
Save robbiejaeger/72cffc1f4770a9cb31e26da98d6001fe to your computer and use it in GitHub Desktop.
// Without the `sendMessage()` promise-based function, but we want to teach them how to handle if something goes wrong here?
if ('serviceWorker' in navigator && 'SyncManager' in window && 'Notification' in window) {
window.addEventListener('load', () => {
fetchLatestHeadlines();
navigator.serviceWorker.register('./service-worker.js')
.then(registration => navigator.serviceWorker.ready)
.then(registration => {
Notification.requestPermission();
$('#submit-article').on('click', function(event) {
let headline = $('#headline-input').val();
let byline = $('#byline-input').val();
navigator.serviceWorker.controller.postMessage({
type: 'add-article',
article: { headline, byline, id: Math.random() }
});
// sendMessage({
// type: 'add-article',
// article: { headline, byline, id: Math.random() }
// });
});
}).catch(err => {
// registration failed :(
console.log(`ServiceWorker registration failed: ${err}`);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment