如何从不同的子域注册服务工作者 [英] how to register a service worker from different sub domain

查看:151
本文介绍了如何从不同的子域注册服务工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个子域名: https://abc.xxxx.com https://xyz.xxxx.com 。所以我的问题是:

I have two subdomains: https://abc.xxxx.com and https://xyz.xxxx.com. So my questions:


1)。是否有可能从 https://xyz.xxxx.com 注册服务工作者< a href =https://abc.xxxx.com =nofollow noreferrer> https://abc.xxxx.com ?如果是,那么如何?

1). is it possible to register a service worker for https://xyz.xxxx.com from https://abc.xxxx.com ? if yes then how?

2)。如果 http://abc.xxxx.com http 不安全),那么无论如何从 https://xyz.xxxx.com
a服务工作者://abc.xxxx.comrel =nofollow noreferrer> http://abc.xxxx.com ,如iframe或其他......

2). if http://abc.xxxx.com (http insecure) then anyway to register a service worker for https://xyz.xxxx.com from http://abc.xxxx.com like in iframe or something....

这是一个真实的情况,我正面临着我的多个子域名。任何帮助赞赏。在此先感谢。

This is a real situation, I am facing for my multiple subdomain. Any help appreciated. Thanks in advance.

推荐答案

以下是一些我认为应该解决您在问题中提出的各点的一般性答案:

Here are some general answers that I think should address the various points you raise in your question:


  • 每个注册的服务人员都有一个关联的 scope ,它指示服务工作者可以控制的网页集。服务工作者的范围是一个URL,该URL必须与注册服务工作者的页面具有相同的来源,并且必须是与与页面相同的路径级别或者一个或多个级别的路径。默认的范围对应于与服务工作者脚本的位置相同的路径级别。由于此限制,无法从一个(子)域上的页面调用 navigator.serviceWorker.register(...),最终得到服务工作者控制另一个(子)域上的页面。

  • Each registered service worker has an associated scope, which dictates the set of web pages that the service worker can control. The scope of a service worker is a URL, and that URL must have the same origin as the page that registers the service worker, and must be either a URL that corresponds to the same path level as the page or a path that's one or more levels down. The default scope corresponds to the same path level as location of the service worker script. Because of this restriction, it's not possible to call navigator.serviceWorker.register(...) from a page on one (sub-)domain and end up with a service worker that controls pages on another (sub-)domain.

有一些限制可以阻止你抛出 https: < iframe> http:页面上,并使用它来注册服务工作者。在https iframe中注册服务工作者时,请参阅 DOMException

There are restrictions in place to prevent you from throwing an https: <iframe> on an http: page and using that to register a service worker. See DOMException when registering service worker inside an https iframe

虽然我不知道它与您的问题直接相关,但显式调用 fetch()对于服务工作者代码中的 http:资源,将导致当前版本的Chrome失败,因为混合内容 fetch() s。我不知道事情是否100%在这方面确定,并且这个开放的bug 仍然相关。

Though I don't know that it's directly related to your question, explicitly calling fetch() for an http: resource within your service worker code will result in a failure in current versions of Chrome, since mixed-content fetch()s are not allowed within a service worker. I don't know if things are 100% settled on that front, and this open bug is still relevant.

如果您的网页同时存在 abc.123。 com xyz.123.com 并且您希望两组页面都由服务工作者控制,那么您需要有两个单独的服务工人注册。每个注册都需要一份服务工作者JS文件的副本,该文件托管在与顶级页面相对应的相应域上,并且所有页面和服务工作者脚本都需要通过 https:

If you have pages that live on both abc.123.com and xyz.123.com and you want both sets of pages to be controlled by a service worker, then you need to have two separate service worker registrations. Each registration needs to be for a copy of your service worker JS file that's hosted on the respective domain corresponding to the top-level page, and all pages and service worker scripts need to be accessed via https:.

话虽如此,您可以通过包含跨域来启动其他域的服务工作者注册页面上有< iframe> ,但主页和< iframe> 都需要通过 HTTPS:。正常的服务工作者范围限制适用,因此,例如,如果您想为另一个域注册一个服务工作者,该服务工作者将覆盖整个 https://other-domain.com/ 范围,您需要确保注册的服务工作者脚本的位置位于顶层,例如 https://other-domain.com/service-worker.js ,而不是 https://other-domain.com/path/to /service-worker.js 。这是 AMP项目通过 < amp-install-serviceworker> 元素

That being said, you can kick off a service worker registration for a different domain by including a cross-domain <iframe> on a page, but both the host page and the <iframe> need to be served via https:. The normal service worker scoping restrictions apply, so if, for example, you want to register a service worker for the other domain that will cover the entire https://other-domain.com/ scope, you need to make sure that the location of the service worker script being registered is at the top-level, e.g. https://other-domain.com/service-worker.js, not at https://other-domain.com/path/to/service-worker.js. This is the approach used by, for example, the AMP project via the <amp-install-serviceworker> element.

这篇关于如何从不同的子域注册服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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