Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wanderview opened this issue Sep 25, 2017 · 10 comments
Assignees
Labels

Comments

@wanderview
Copy link
Member

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
Copy link
Contributor

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
Copy link
Member Author

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

@jakearchibald
Copy link
Contributor

Not sure about implementation. I'm looking at changing that functional event algorithm btw, but not that particular behaviour #1199

@wanderview
Copy link
Member Author

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

@jakearchibald
Copy link
Contributor

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

@mfalken
Copy link
Member

mfalken commented Nov 7, 2017

Chrome just does the optimization for fetch currently.

@jakearchibald
Copy link
Contributor

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

@jakearchibald
Copy link
Contributor

Action: As before. Make it a may.

@mfalken mfalken self-assigned this Jun 3, 2019
mfalken added a commit that referenced this issue Jun 6, 2019
…1417)

This makes {install,activate,fetch,message} events and
functional events use the same algorithm to determine
whether to skip firing the event, and makes it a may
rather than must.

It also unifies the concept of a stale registration to reduce
duplicated text.

Addresses #1200
@mfalken
Copy link
Member

mfalken commented Jun 6, 2019

Addressed in #1417

@mfalken
Copy link
Member

mfalken commented Nov 5, 2019

At whatwg/dom#653 @annevk argues it should be limited to fetch or should be a must rather than a may.

@mfalken mfalken reopened this Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants