如何防止同一服务工作者注册多个页面? [英] How to prevent the same service worker from registering over multiple pages?

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

问题描述

我有一个服务工作者脚本,它在多个站点级别上重复注册。

I have a service worker script that is registering repeatedly over multiple site levels.

换句话说,同一服务工作者已在www.site.ca/注册, www.site.ca/text-text,www.site.ca/example-example,等等。

In other words the same service worker is registered for www.site.ca/, www.site.ca/text-text, www.site.ca/example-example, and etc.

该网站建立在php上,根据不同的网址生成,具体取决于关于内容如此类似API。服务工作者注册在这些页面上的原因是因为大多数站点流量落在这些页面而不是主页上。结果是同一服务工作者在不同页面上注册了不同的ID。

The site is built on php with different urls being produced depending on the content so kind of API-like. The reason that the service worker registration is on those pages is because most of the site traffic lands on those pages rather than the home page. The result being the same service worker being registered with different ids over different pages.

有没有人有办法阻止在多个子级别上多次注册同一个脚本?

Does anyone have a way to prevent multiple registrations of the same script over multiple sub levels?

编辑:

即使用户不在网站上,服务工作者的目的也是设置通知。此外,我所遇到的问题会对将来更新服务工作者的任何尝试产生负面影响,因为要更新多个副本。结果是当用户在特定页面上再次登陆站点时,只有一个副本会被更新。无法保证该服务工作文件的所有副本都将得到更新。此外,多个副本的结果导致用户从网站获得一堆通知,因此这是一个问题。

The purpose of the service worker is to set up notifications even if the user isn't on the site. Also the issue I'm having would negatively affect any attempts to update the service worker in the future since there would be multiple copies to update. The result being that only one copy would ever be updated when the user lands in the site again on a specific page. There would be no guarantee that all the copies of that service worker file would be updated. Also the result of the multiple copies has resulted in users getting stacks of notifications from the site so it is a problem.

编辑:

以下是我到目前为止的情况,我的理解范围定义了它的注册位置。然而问题是它需要能够在人进入网站的任何地方进行注册,因此范围设置为'/'。

The below is what I have so far and to my understanding scope defines where it gets registered. However the problem is that it needs to be able to register wherever a person enters the site thus the scope was set to '/'.

我要解决的问题是防止后续相同服务工作文件的寄存器。

The issue I want to solve is to prevent subsequent registers of the same service worker file.

Localhost是实际网站的占位符。

Localhost is a placeholder for the actual site.

navigator.serviceWorker.register('localhost/service-worker.js', {scope: './'})
.then(initialiseState);

在用于调试的Chrome开发工具中,生成的服务工作者的格式如下:

In Chrome dev tools for debugging, the resulting service workers are in the format like this:

范围:localhost / url / stuff /

注册ID:110

活跃工人:

安装状态:ACTIVATED

运行状态:STOPPED

脚本:localhost / service-worker.js

...

Scope: localhost/url/stuff/
Registration ID: 110
Active worker:
Installation Status: ACTIVATED
Running Status: STOPPED
Script: localhost/service-worker.js
...

范围:localhost /

注册ID:111

活跃工人:

安装状态:ACTIVATED

运行状态: STOPPED

脚本:localhost / service-worker.js

...

Scope: localhost/
Registration ID: 111
Active worker:
Installation Status: ACTIVATED
Running Status: STOPPED
Script: localhost/service-worker.js
...

范围:localhost / url

注册ID:112

活跃工人:

安装状态:ACTIVATED

运行状态:STOPPED

脚本:localhost /service-worker.js

...

Scope: localhost/url
Registration ID: 112
Active worker:
Installation Status: ACTIVATED
Running Status: STOPPED
Script: localhost/service-worker.js
...

理想情况下,我想以某种方式将它保存到一个条目。

Ideally I want to somehow just keep it to one entry somehow.

推荐答案

根据 MDN ServiceWorkerContainer.register 方法接受选项的第二个参数:

According to MDN, ServiceWorkerContainer.register method takes a second parameter of options:


目前可用的选项是:

Currently available options are:


  • 范围:表示URL的USVString定义服务工作者的注册范围;服务工作者可以控制哪些URL范围。这通常是一个相对URL,当
    未指定时默认为'/'。






关于您的更新问题, W3C服务工作者WD 说:


当一个
已经存在时,服务工作者注册相同范围的URL用户代理导致现有服务工作者
注册被替换。

A service worker registration of an identical scope url when one already exists in the user agent causes the existing service worker registration to be replaced.

因此 navigator.serviceWorker。注册(url,{scope:'/'})。然后(doSomething)应该有效。

问题在于范围示例中的网址:

./ 应为 /

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

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