Angular6 Service Worker and background sync

陌路散爱 提交于 2019-12-30 06:18:07

问题


it seems that angular6 does not support background syncing with service worker. What are the steps required to do this without any library in an angular service?

https://developers.google.com/web/updates/2015/12/background-sync

How and where can I access WorkerGlobalScope in an angular6 application directly so that background sync can be done.:

self.addEventListener('sync', function(event) {
  if (event.tag == 'myFirstSync') {
    event.waitUntil(doSomeStuff());
  }
});

The problem is that the angular CLI generates the servicworker file (ngsw-worker.js). Is there a way to inject / modify / extend this file? Yes, i can edit the file ngsw-worker.js by hand or with another fancy trick. Is there an official way to do this?


回答1:


Yes you can use your custom script to extend the capabilities of Angular's service worker.

Just follow below steps:

  1. Create a js file with your custom script let's say sw-custom.js under src directory of your project.
  2. Add "importScripts('./ngsw-worker.js')" at top of sw-custom.js file.
  3. Add sw-custom.js file to angular.json file under scripts section.
  4. Now register your sw-custom.js file in app module instead of

ngsw-worker.js

That's it now you can write your script here as per your need.

Maybe it is not correct approach but it works.

Here is the blog for same.



来源:https://stackoverflow.com/questions/51449020/angular6-service-worker-and-background-sync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!