Re: [w3c/ServiceWorker] Promise on navigator.serviceWorker that resolves when page is controlled (#799)

For people who're reading this now (through Google search), the `workbox-window` package now includes `active` and `controlling` promises which can be used.

Usage:
```
// somewhere
const workbox = new Workbox('/service-worker.js');

// somewhere else
await workbox.active
await workbox.controlling
```

Excerpt from the source code at time of writing:
```

  /**
   * Resolves to the service worker registered by this instance as soon as it
   * is active. If a service worker was already controlling at registration
   * time then it will resolve to that if the script URLs (and optionally
   * script versions) match, otherwise it will wait until an update is found
   * and activates.
   *
   * @return {Promise<ServiceWorker>}
   */
  get active() {
    return this._activeDeferred.promise;
  }

  /**
   * Resolves to the service worker registered by this instance as soon as it
   * is controlling the page. If a service worker was already controlling at
   * registration time then it will resolve to that if the script URLs (and
   * optionally script versions) match, otherwise it will wait until an update
   * is found and starts controlling the page.
   * Note: the first time a service worker is installed it will active but
   * not start controlling the page unless `clients.claim()` is called in the
   * service worker.
   *
   * @return {Promise<ServiceWorker>}
   */
  get controlling() {
    return this._controllingDeferred.promise;
  }
  ```
  
  Although it is a very small letdown that there's no `workbox.ready` alias for `navigator.serviceWorker.ready`, to keep it all nicely tucked into one place 😇 

-- 
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/799#issuecomment-809543196

Received on Monday, 29 March 2021 16:57:30 UTC