简体   繁体   English

Service Worker 更新延迟

[英]Service worker update delay

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.我有一个正在运行的网络应用程序,它依靠服务工作者来检查所有缓存的文件并确保用户使用正确版本的应用程序。

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.我们的客户目前希望设备在特定时间点(重新打开应用程序时)等检查更新,因为目前当您打开应用程序时,设备可能需要长达 5 分钟的时间才能实现其过时的版本。

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?我是否可以强制设备检查 Service Worker 是否有任何新更改,而不是等待应用程序检查我?

Thanks!谢谢!

There are two main points you should refine for instant update of your SW.为了即时更新您的软件,您应该改进两个要点。

Shortly不久

  1. Call your sw.js also from more often visited pages than your app's index page.也可以从比您的应用程序的索引页面更常访问的页面调用您的sw.js
  2. Utilize skipWaiting() method to activate your SW instantly.利用skipWaiting()方法立即激活您的软件。

In detail详细

1) navigator.serviceWorker.register method call 1) navigator.serviceWorker.register方法调用

You should note that newer verison of the SW will only be checked by a navigator.serviceWorker.register() call.您应该注意,更新版本的软件只能通过navigator.serviceWorker.register()调用进行检查。

And typically register() method will be called in one of your pages only.通常register()方法只会在您的页面之一中调用。 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.因为如果用户只是在没有关闭 PWA 的情况下打开另一个应用程序,然后在 2 小时后返回到您的应用程序,他们将避开您的register()调用可能所在的应用程序的索引页面。

So, my suggestion is to put it to many pages if not every.所以,我的建议是把它放在很多页上,如果不是每页的话。 Downside is clients will make much more calls to sw.js .缺点是客户会更多地调用sw.js Upside is clients will retrieve last version of SW without losing time.好处是客户端可以在不浪费时间的情况下检索最新版本的软件。

2) Activating service worker 2) 激活 service worker

Service Worker's activate event will be called when SW no longer has any active clients .当 SW 不再有任何活动客户端时,将调用 Service Worker 的activate事件。 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:请参阅MDN关于此事的引用

..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.一旦不再加载此类页面,新的 Service Worker 就会激活。

And solution to this one would be using skipWaiting() method in conjunction with Clients.claim()解决这个问题的方法是将skipWaiting()方法与Clients.claim()结合使用

Usage example of those taken from MDN.取自 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());
});

Expiry directive in .htaccess on asset server资产服务器上.htaccess到期指令

Make sure that the browser cache expiry directive set by the .htaccess file on your asset server is set to a really short duration or even to none as explained here .确保由资产服务器上的.htaccess文件设置的浏览器缓存到期指令设置为非常短的持续时间,甚至设置为,如此处所述 Failing to do so, will result in the browser not seeing any change for a while in the manifest.json , nor the service-worker.js .如果不这样做,将导致浏览器在一段时间内看不到manifest.jsonservice-worker.js中的任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM