Skip to content

Instantly share code, notes, and snippets.

@moshfeu
Created August 30, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moshfeu/f0079d46fcfd1ec493db27cf0a91f4c1 to your computer and use it in GitHub Desktop.
Save moshfeu/f0079d46fcfd1ec493db27cf0a91f4c1 to your computer and use it in GitHub Desktop.
Send data to the Service Worker
<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>
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