Re: [w3c/ServiceWorker] [Proposal] ServiceWorkerContainer.installing(scope) promise (#1364)

I think I'd need to see more clarity in the "Motivation" section. At a minimum, I have these questions:

1) Why do you want to observe the whole service worker lifecycle? In #1222, separately filed as #1247, @gauntface suggested an `updatestatechange ` event listener API for doing so, but I was skeptical, because I didn't really see the point. I still don't. It sounds like error tracking and/or performance tracking was all anybody came up with, but all of that stuff is best done inside the SW script itself, not in client-side `window` code.

2) Why do you want to build this as a web component? As you've noticed, you've created a problem for yourself by having to wait for `connectedCallback`. But why do you want to use a WC here at all? Instead of trying to `getRegistration()` and hoping that you call it after someone else called `register()`, it seems like you should just attach this code to the `register()` promise handler.

But let's suppose you say "Very well, I now see that I didn't really want this to be a WC at all; I really just wanted to prolyfill `updatestatechange`. I'll use the registration provided to me by `register()`. Now please tell me how."

Per #1247 I think you can do what you want with code like this:

```js
function listenForStateChanges(reg, callback) {
  if (reg.installing) reg.installing.addEventListener('statechange', callback);
  if (reg.waiting) reg.waiting.addEventListener('statechange', callback);
  reg.addEventListener('updatefound', function() {
    reg.installing.addEventListener('statechange', callback);
  });
}
navigator.serviceWorker.register('/sw.js').then(function(reg) {
  listenForStateChanges(reg);
});
```

But, again, if you're just using code like this for performance logging and/or error tracking, I recommend not doing this at all. Just put your tracking code in the SW script.

"But what if the SW script fails?" Feel free to track _registration_ failures when you call `register()` client-side. But that's _all_ you need to track client side. Track everything else in the SW.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1364#issuecomment-434015206

Received on Monday, 29 October 2018 18:04:58 UTC