-
Notifications
You must be signed in to change notification settings - Fork 312
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
Feature detection for type="module" support #1582
Comments
A workaround for now: let readType = false;
navigator.serviceWorker
.register('about:blank', {
get type() {
readType = true;
},
})
.catch(() => {}); If |
While that does detect In the case of Chrome 90, there's an exception throw immediately ( In the case of Safari 14.0.3, the ES module version of the script downloads and then fails to parse, with a |
sighhhhh that Safari issue is frustrating. |
https://static-misc-2.glitch.me/detect-sw-module-support/ - here's a test that works today, although it requires a network request. |
Epic 😄 It's good to have that to refer folks to if they want to use ES modules today and also want to avoid a potential double-download. Long-term, is a more official mechanism for detecting support reasonable to add to the service worker specification? Actually, since ES modules in dedicated workers are already supported in Chrome and Safari, how do developers using them deal with what I'm assuming is a similar issue? (EDIT: Although I guess code in the |
It'd be nice to be able to detect it without starting up a worker. Although, at least with a worker you can avoid the network request. But yeah, it'd be good to have the same solution in both places. Going to see if I can make the test better, so it might be broken for a bit… |
Maybe not a great general solution, but would there be any benefit in changing the |
I couldn't make it better. I thought Safari might support the
That seems nice! |
If #1585 is resolved, and dynamic You could theoretically create four different versions of a service worker script— Exposing some combination of |
Should we pursue this? |
I think a client-side solution would be a more general solution. Might not be worth spending time on the header thing yet? |
I've recently been experimenting with ES modules for service workers, 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:
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 usesimportScripts()
, 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 theServiceWorkerContainer
, that could answer the "are ES module service workers supported?" question in advance of attempting the registration. Either a boolean that wastrue
when ES modules are supported, or alternatively, a list of supportedtype
values, set in this case to['classic', 'module']
, if we think that there might be additionaltype
s that will be added in the future.The text was updated successfully, but these errors were encountered: