Angular Service Worker on the browsers that do not

2019-04-12 12:05发布

问题:

Angular team made a great job and introduced service workers support in the version 5. The documentation is quite helpful and I can really see how the things will work right after reading the related articles, however there is one topic that is not covered there. How does this all work in the browsers that do not support service workers.

This is what's being said:

Your application must run in a web browser that supports service workers. Currently, the latest versions of Chrome and Firefox are supported. To learn about other browsers that are service worker ready, see the Can I Use page.

Can I Use shows quite a poor support of the feature in relatively important browsers. I can survive the not working IE11, but Safari (especially mobile) is a thing I wouldn't like to keep unsupported.

So, my intention would be to know how the angular service worker module, services, events those services provide etc will behave on the browser that does not support service workers.

Will all those just silently fail? Will the events be triggered anyhow? Will the ngsw-worker.js be requested? Will something be cached in IndexedDB instead of service worker?

The solution looks cool, however I cannot answer the question should I use it? until I know the answers on the questions above.

回答1:

The official response

The Angular SW only works in browsers which support service workers. This includes all of the application integration (e.g. the SwUpdate service).

If SWs are unsupported in the current browser.

  • The SW script and ngsw.json will never be downloaded.
  • Active attempts to interact with the SW, such as calling -
  • SwUpdate.checkForUpdate() will return rejected promises.
  • Passive attempts to listen to SW Observables such as SwUpdate.available will simply never trigger - the Observables will never emit.


回答2:

Basically browsers that doesn't support service workers will silently fail. So anything in the background connected to the SW such as offline caching and push notifications will not work. The site will still work like any other site, just that the functionalities and features of PWA will not.