服务人员更新延迟 [英] Service worker update delay

查看:88
本文介绍了服务人员更新延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的Web应用程序,并依赖服务工作人员来检查所有缓存的文件,并确保用户使用正确版本的应用程序。

I have a web app that is working and relying on service workers to keep all the cached files in check and to make sure users are on the correct version of the app.

我们的客户当前希望设备检查特定点(重新打开应用时)等时的更新,因为当前打开应用时,设备最多可能需要5分钟才会在过期版本上实现。

Our client is currently wanting the device to check for an update at specific points (When reopening the app) etc, as currently when you open the app it can take up to 5minutes before the device realises its on an outdated version.

我是否可以强制设备检查服务工作者是否有任何新的更改,而不是等待应用程序检查我?

Am I able to force the device to check the service worker for any new changes instead of waiting for the app to check for me?

谢谢!

推荐答案

您应该优化两个要点,以便即时更新SW。

There are two main points you should refine for instant update of your SW.

短暂


  1. 致电 sw.js 来自更常访问的网页,而不是应用的索引页。

  2. 利用 skipWaiting() 方法立即激活您的软件。

  1. Call your sw.jsalso from more often visited pages than your app's index page.
  2. Utilize skipWaiting() method to activate your SW instantly.

详细

1) navigator.serviceWorker.register 方法调用

1) navigator.serviceWorker.register method call

您应该注意,SW的较新版本只能通过 navigator.serviceWorker.register() 打电话。

You should note that newer verison of the SW will only be checked by a navigator.serviceWorker.register() call.

通常 register()方法只会在你的一个页面中调用。如果是这种情况,则应将该命令添加到所有页面。因为如果用户只是在没有关闭PWA的情况下打开另一个应用程序然后在2小时之后返回到您的应用程序,他们将避免应用程序的索引页面 register()电话可能是。

And typically register() method will be called in one of your pages only. If that is the case, you should add that command to all of your pages. Because if user just opens another app without closing your PWA and then returns to your app after, say 2 hours, they will avoid the index page of the app where your register() call probably is.

所以,我的建议是把它放到很多页面,如果不是每一个。下行是客户将拨打更多来电 sw.js 。上行是客户将在不浪费时间的情况下检索上一版本的SW。

So, my suggestion is to put it to many pages if not every. Downside is clients will make much more calls to sw.js. Upside is clients will retrieve last version of SW without losing time.

2)激活服务人员

当SW不再有任何活动时,将调用服务工作者的激活事件 clients 。因此,新应用程序仅在应用程序的每个实例之后激活,但一个已关闭,其余选项卡将刷新/一个选项卡重新打开。请参阅 MDN引用有关此事:

Service Worker's activate event will be called when SW no longer has any active clients. So new version will be activated only after every instance of the app but one closed, and the remaining tab is refreshed/one tab re-opened. See quote from MDN about the matter:


..新版本在后台安装,但尚未激活。
只有当不再加载任何页面时,才会激活
仍在使用旧服务工作者。只要还没有
这样的页面仍然加载,新服务工作者就会激活。

..the new version is installed in the background, but not yet activated. It is only activated when there are no longer any pages loaded that are still using the old service worker. As soon as there are no more such pages still loaded, the new service worker activates.

并解决这个问题。将使用 skipWaiting() 方法与 <一起使用code> Clients.claim()

And solution to this one would be using skipWaiting() method in conjunction with Clients.claim()

从MDN获取的用法示例。 点击以在该页面上查看更多相关信息:

Usage example of those taken from MDN. Click to see more about it on that page:

self.addEventListener('install', function(event) {
  event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function(event) {
  event.waitUntil(self.clients.claim());
});

这篇关于服务人员更新延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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