Can I have multiple service workers both intercept the same fetch request?

前端 未结 1 499
鱼传尺愫
鱼传尺愫 2021-01-31 16:42

I\'m writing a library which I will provide for 3rd parties to run a service worker on their site. It needs to intercept all network requests but I want to allow them to build t

相关标签:
1条回答
  • 2021-01-31 17:11

    No, you can not. Only one service worker per scope is allowed to be registered so the latest kick the previous one out unless the scope is more specific, in this case, the request is attended by the most specific only.

    Nevertheless, you can attach multiple fetch handlers and they all will process the request so maybe you can write your functionality in a separated script and let the user's service worker to include your file via importScripts().

    The first handler calling event.respondWith() synchronously (actually, you can not call this method asynchronously) wins and the remaining handlers trying to call will throw.

    Prioritization and coordination requires middleware. You can check ServiceWorkerWare or sw-toolbox.

    0 讨论(0)
提交回复
热议问题