[w3c/ServiceWorker] Feature detection for type="module" support (#1582)

I've recently been experimenting with [ES modules for service workers](https://chromestatus.com/feature/4609574738853888), now supported in pre-stable versions of Chrome and Safari.

I've found that without a way to feature detect support, writing code that's backwards compatible can be awkward, and in some browsers, less performant.

Roughly, this is the registration snippet that I was trying out:

```js
async function registerSW() {
  try {
    await navigator.serviceWorker.register('es-module-sw.js', {
      type: 'module',
    });
  } catch(error) {
    await navigator.serviceWorker.register('import-scripts-sw.js');
  }
}
```

Pre-stable versions of Chrome and Safari will successfully run the first registration, and the current stable versions of Chrome will raise an exception immediately because it "knows" that `type: 'module'` isn't supported.

However, the current stable versions of Firefox and Safari will download and attempt to execute the ES module flavor of service worker, and only raise an exception when there's a syntax error due to the usage of ES module imports. The fallback logic in the `catch` can successfully register a classic version of the service worker that uses `importScripts()`, but not until after spending the bandwidth and time needed to download and parse something that we should know in advance won't work.

Am I missing something already implemented in all browsers that would make it possible to feature-detect `type: 'module'` support ahead of time? If not, consider this a feature request to add in a property, presumably exposed on the `ServiceWorkerContainer`, that could answer the "are ES module service workers supported?" question in advance of attempting the registration. Either a boolean that was `true` when ES modules are supported, or alternatively, a list of supported `type` values, set in this case to `['classic', 'module']`, if we think that there might be additional `type`s that will be added in the future.

-- 
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/1582

Received on Thursday, 15 April 2021 13:40:12 UTC