You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Web API type definition issue] Mark navigator.serviceWorker as an optional property #1987
APIs like these are marked non-optional for convenience, since it's extremely plausible that your script runs only in secure contexts as an invariant.
Based on his comment, it appears to me that it had been misunderstood that serviceWorker is undefined in non-secure contexts, while the point is that it may be undefinedeven in secure contexts.
Examples include but are not limited to:
Firefox's private mode
Some in-app browsers, especially those on iOS.
In-app browser usage is not insignificant!
~62% of global web traffic comes from mobile devices, and, according to eMarketer, ~88% of the time people spend in their mobile devices is used on apps. Apps today incorporate IABs for the purposes of authentication, advertisement, and more. IAB usage is at its peak in social media and messaging apps.
Given the high usage of IABs. serviceWorker not being an optional property of navigator is anything but convenient.
Hello Stavros Tsioulis (@stavros-tsioulis)
This is intentional. There are a lot of calls like this that only work in a secure context. If you know your code is always going to run over HTTPS, it would be incredibly annoying and counterproductive if the compiler forced you do either do a null check or add a non-null assertion to every such call.
I am always assuming code to be running over HTTPS. However, even so, it is still not true that serviceWorker is defined in all secure contexts!
For example, here is the page on MDNfalsely claiming that navigator.serviceWorker is available so long as window.isSecureContext is true.
Try to open firefox on a private window and you'll find out that window.isSecureContext is true, while navigator.serviceWorker is undefined.
The same is true for certain IABs used on apps like Messenger.
Contradictorily to the previous MDN page, the one for navigator.serviceWorkerclarifies that this feature may not be available in some secure contexts.
Basically: navigator.serviceWorker may be undefined over HTTPS. There are secure contexts where this property is undefined, which are not so rare or niche where convenience of non-optionality is a better option.
Hello Stavros Tsioulis (@stavros-tsioulis)
In this case, it is better to file an issue upstream in MDN. Once that issue is resolved, it should automatically reflect here.
Making this optional will disturb a lot of codes where the existence of service worker can be safely assumed, so this library doesn't make it optional unless the spec says so.
Hi Kagami Sascha Rosylight (@saschanaz) IAB stands for in-app browser. If the typings of this API are to fully reflect the spec, there is probably nothing more to discuss here.
I also stumbled upon https://caniwebview.com/search/?s=serviceWorker which reveals a much wider range of APIs, especially those connected to navigator, that are unavailable on various WebViews. I can see how it would be untenable to maintain overrides based simply on arbitrary decisions of implementers to deactivate such APIs, against the spec.
I really miss the conditional compilation (but only for type libs), it could save a lot of complexity here, where the users would be able to just compile with webview requirement. 🥺
Activity
Bashamega commentedon Apr 18, 2025
Hello Stavros Tsioulis (@stavros-tsioulis)
This is intentional. There are a lot of calls like this that only work in a secure context. If you know your code is always going to run over HTTPS, it would be incredibly annoying and counterproductive if the compiler forced you do either do a null check or add a non-null assertion to every such call.
stavros-tsioulis commentedon Apr 18, 2025
Hi there Adam Naji (@Bashamega) .
I am always assuming code to be running over HTTPS. However, even so, it is still not true that
serviceWorkeris defined in all secure contexts!For example, here is the page on MDN falsely claiming that
navigator.serviceWorkeris available so long aswindow.isSecureContextistrue.Try to open firefox on a private window and you'll find out that
window.isSecureContextis true, whilenavigator.serviceWorkerisundefined.The same is true for certain IABs used on apps like Messenger.
Contradictorily to the previous MDN page, the one for
navigator.serviceWorkerclarifies that this feature may not be available in some secure contexts.Basically:
navigator.serviceWorkermay be undefined over HTTPS. There are secure contexts where this property is undefined, which are not so rare or niche where convenience of non-optionality is a better option.Bashamega commentedon Apr 19, 2025
Hello Stavros Tsioulis (@stavros-tsioulis)
In this case, it is better to file an issue upstream in MDN. Once that issue is resolved, it should automatically reflect here.
stavros-tsioulis commentedon Apr 19, 2025
Thanks Adam Naji (@Bashamega)
Could you perhaps help me pinpoint what part in MDN should be changed?
Bashamega commentedon Apr 22, 2025
I am not sure Stavros Tsioulis (@stavros-tsioulis)
Maybe Kagami Sascha Rosylight (@saschanaz) can help
saschanaz commentedon Apr 22, 2025
First of all, what is IAB?
Firefox not providing Service Worker in PBM is not a feature but rather a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1320796
Making this optional will disturb a lot of codes where the existence of service worker can be safely assumed, so this library doesn't make it optional unless the spec says so.
stavros-tsioulis commentedon Apr 23, 2025
Hi Kagami Sascha Rosylight (@saschanaz) IAB stands for in-app browser. If the typings of this API are to fully reflect the spec, there is probably nothing more to discuss here.
I also stumbled upon https://caniwebview.com/search/?s=serviceWorker which reveals a much wider range of APIs, especially those connected to
navigator, that are unavailable on various WebViews. I can see how it would be untenable to maintain overrides based simply on arbitrary decisions of implementers to deactivate such APIs, against the spec.saschanaz commentedon Apr 23, 2025
Perhaps there can be separate libs for webviews.
I really miss the conditional compilation (but only for type libs), it could save a lot of complexity here, where the users would be able to just compile with webview requirement. 🥺