Created
July 12, 2017 00:19
-
-
Save robbiejaeger/72cffc1f4770a9cb31e26da98d6001fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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