Skip to content

Latest commit

 

History

History
17 lines (12 loc) · 565 Bytes

send-message-to-service-worker.md

File metadata and controls

17 lines (12 loc) · 565 Bytes

Use postMessage API to send messages to Service Worker

Assuming your page is controlled by a Service Woker, you can access it via the navigator.serviceWorker.controller property and use the postMessage API to send a message.

navigator.serviceWorker.controller.postMessage({myMessage: "hello"});

Inside your service worker code, you can listen for that message with:

self.addEventListener('message', (event) => {
  // ...
})

See service worker documentation.