Created
August 30, 2019 15:50
-
-
Save moshfeu/f0079d46fcfd1ec493db27cf0a91f4c1 to your computer and use it in GitHub Desktop.
Send data to the Service Worker
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
<button onclick="sendMessage()">Send message</button> | |
<script> | |
let se; | |
const registration = navigator.serviceWorker.register('/sw.js').then(data => { | |
se = data.active; | |
console.log('ready'); | |
}); | |
function sendMessage() { | |
se.postMessage('message from client'); | |
} | |
</script> |
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('message', ({data}) => { | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment