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

JeanSebTr left a comment (w3c/ServiceWorker#1582)

I've managed to get feature detection working.

The trick to avoid the unwanted network request is to rely on the same-site restriction of ServiceWorker registration. Browsers that support `type: "module"` read the getter trap before throwing a `SecurityError`.

For the Safari 14 false positive, we can use another feature released in Safari 15: `aspect-ratio`, which was already supported by every other browser by the time they added support for module Service Workers.


```js
async function serviceWorkerSupportsModuleType() {
  const url = new URL("sw.js", window.location.href);
  url.hostname = "security-error-trigger." + url.hostname;
  let supports = false;
  try {
    await navigator.serviceWorker.register(url.href, {
      get type() {
        supports = true;
      },
    });
  } catch (e) {
    // Chrome 80-90
    // DOMException: type 'module' in RegistrationOptions is not implemented yet.See https://crbug.com/824647 for details.
    if (
      e.name === "NotSupportedError" &&
      e.message.includes(
        "type 'module' in RegistrationOptions is not implemented yet"
      )
    ) {
      supports = false;
    }
  }

  // Safari 14 will read the type property despite not supporting "module"
  // Fortunately Safari 15 also introduced support for aspect-ratio
  // see: https://developer.apple.com/documentation/safari-release-notes/safari-15-release-notes#CSS
  if (supports && !CSS.supports("aspect-ratio", "1")) {
    return false;
  }
  return supports;
}
```

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

Message ID: <w3c/ServiceWorker/issues/1582/5511913651@github.com>

Received on Wednesday, 2 September 2026 15:20:57 UTC