This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The startMessages()
method of the ServiceWorkerContainer
interface explicitly starts the flow of messages being dispatched from a service worker to pages under its control (e.g. sent via Client.postMessage()
). Can be used to avoid race conditions created when a service worker tries to post a message to a page before that page has actually finished loading an ServiceWorkerContainer.onmessage
handler to process them..
Messages sent from the service worker to a document would get stored in its client message queue, and would previously start getting dispatched when one of two things occur:
ServiceWorkerContainer.onmessage
is set up to handle the messages.DOMContentLoaded
).There is a possible race condition here — if the page content has finished loading before the onmessage
handler has been set, the messages could be lost because there is nothing to handle it.
startMessages()
has been implemented to mitigate this race condition. If you want message sending to start before the page content has loaded, you can explicitly call this method.
ServiceWorkerContainer.startMessages();
None.
Void.
if('serviceWorker' in navigator) { navigator.serviceWorker .register('/sw.js') .then(function() { console.log('Service Worker Registered'); }); } ... navigator.serviceWorker.startMessages(); navigator.serviceWorker.onmessage() { ... };
Specification | Status | Comment |
---|---|---|
Service Workers The definition of 'ServiceWorkerContainer: startMessages()' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | ? | ? | ? | ? | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | ? | ? | ? | ? | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/startMessages