How to load Javascript file in a Service Worker dynamically?

旧时模样 提交于 2020-06-15 18:28:56

问题


I can't use any solutions that involve creating a <script> tag dynamically because document does not exist in a ServiceWorker context.

In this case I want to load analytics toolkit so I can track when SW-triggered Notifications are interacted with by the user.

Any ideas?


回答1:


That sounds like the intended use case of the importScripts() method.

The imported script shares the global scope with your service worker, so modifying self via, e.g., self.addEventListener() or self.someIdentifier = 'blah' will have the same effect as it would from within the main service worker script.



来源:https://stackoverflow.com/questions/36251929/how-to-load-javascript-file-in-a-service-worker-dynamically

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