w3c / ServiceWorker

Service Workers
https://w3c.github.io/ServiceWorker/
Other
3.63k stars 316 forks source link

is the browser required to start the ServiceWorker for postMessage() if there is no message event handler? #1200

Open wanderview opened 7 years ago

wanderview commented 7 years ago

Consider this code:

// main window script
let bc = new BroadcastChannel('sw_side_channel');
sw.onmessage = evt => console.log('service worker started up');
navigator.serviceWorker.controller.postMessage('ping');

// service worker script
let bc = new BroadcastChannel('sw_side_channel');
bc.postMessage('pong');
self.onmessage = null;  // no direct message event handler

Is the browser required to start the service worker if its not running and a postMessage() is fired at while there is no message event handler? Worker startup is observable thanks to things like BroadcastChannel or Clients API.

Or can the browser do similar optimizations like we do for FetchEvent where we don't start the worker if there is no message handler?

jakearchibald commented 7 years ago

I think we should do it for all events.

https://w3c.github.io/ServiceWorker/#handle-functional-event-algorithm covers it for "functional events", but "message" isn't one of those. We should be consistent here.

wanderview commented 7 years ago

Hmm, I didn't realize this mechanism had been expanded beyond FetchEvent. Does chrome do this for other events yet?

jakearchibald commented 7 years ago

Not sure about implementation. I'm looking at changing that functional event algorithm btw, but not that particular behaviour https://github.com/w3c/ServiceWorker/pull/1199

wanderview commented 7 years ago

I just thought making event handlers observable at all was contentious so we limited it to fetch.

jakearchibald commented 7 years ago

I don't have strong feelings here. Happy to make it just fetch.

mfalken commented 6 years ago

Chrome just does the optimization for fetch currently.

jakearchibald commented 6 years ago

Action: We should make this behaviour optional in the spec, but allow it for any top level event, including message.

jakearchibald commented 5 years ago

Action: As before. Make it a may.

mfalken commented 5 years ago

Addressed in https://github.com/w3c/ServiceWorker/pull/1417

mfalken commented 4 years ago

At https://github.com/whatwg/dom/pull/653 @annevk argues it should be limited to fetch or should be a must rather than a may.