我可以让多个 service worker 拦截同一个 fetch 请求吗? [英] Can I have multiple service workers both intercept the same fetch request?

查看:57
本文介绍了我可以让多个 service worker 拦截同一个 fetch 请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个库,我将提供给 3rd 方在他们的站点上运行 Service Worker.它需要拦截所有网络请求,但如果他们愿意,我希望允许他们构建自己的 Service Worker.

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 their own service worker if they like.

我可以让两个 Service Worker 拦截相同的 fetch 并在它们之间提供某种优先级/排序吗?或者我应该使用其他一些模式吗?

Can I have both service workers intercept the same fetches and provide some kind of priority/ordering between them? Alternatively is there some other pattern I should be using?

谢谢

推荐答案

不,你不能.每个范围只允许注册一个 service worker,所以 最新的踢上一个out 除非范围更具体,在这种情况下,请求仅由最具体的人参与.

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.

尽管如此,您可以附加多个 fetch 处理程序,它们都会处理请求,所以也许您可以在单独的脚本中编写您的功能,并让用户的服务工作者通过 importScripts().

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().

第一个同步调用 event.respondWith() 的处理程序(实际上,您不能异步调用此方法)获胜,其余尝试调用的处理程序将抛出.

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.

优先级和协调需要中间件.您可以查看 ServiceWorkerWaresw-工具箱.

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

这篇关于我可以让多个 service worker 拦截同一个 fetch 请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆