新的更新可用消息和发布操作 [英] New update available message and post action

查看:50
本文介绍了新的更新可用消息和发布操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来检查服务工作者的变化

I have this code which checks for changes in service worker

if ("serviceWorker" in navigator) {
    window.addEventListener("load", function() {
        navigator.serviceWorker.register(worker).then(function(reg) {
            reg.onupdatefound = function() {
                var installingWorker = reg.installing;

                installingWorker.onstatechange = function() {
                    switch (installingWorker.state) {
                        case 'installed':
                            if (navigator.serviceWorker.controller) {
                                console.log('New or updated content is available.');
                            } else {
                                console.log('Content is now available offline!');
                            }
                            break;
                    }
                };
            };
        }).catch(function(e) {
            console.error('Error during service worker registration:', e);
        });
    })
}

看了很多文档才知道,只有重新打开已安装的应用程序才会更新版本.是否有任何 javascript 事件可以重新加载或重新打开已安装的应用程序,以便我可以在更新可用时显示一个单击按钮.

By referring many documents, I came to know that, the version will be updated only once the installed app is reopened again. Is there any javascript event to reload or reopen the installed app so that I can show a button to click when update is available.

推荐答案

如果您使用的 Service Worker 主动预缓存版本化 URL,并且仅提供与实际 Service Worker 关联的每个 URL 的版本,那么您d 需要告诉新的,等待"服务工作者跳过等待";并在新的缓存响应集生效之前激活.一旦新的 Service Worker 跳过等待,您需要重新加载所有打开的客户端,以便它们获得新资源.

If you're using a service worker that aggressively precaches versioned URLs, and serves only the versions of each URL that's associated with the actual service worker, then you'd need to tell the new, "waiting" service worker to "skip waiting" and activate before the new set of cached responses will go into effect. Once the new service worker skips waiting, you'd want to reload all open clients so that they get the new resources.

这需要页面的 JavaScript 之间的协调以及向您的服务工作者添加 message 处理程序.处理这种协调的一个方法可以在工作箱文档.(即使您不使用 Workbox 进行预缓存,也可以使用它.)

This requires coordination between the page's JavaScript as well as adding a message handler to your service worker. One recipe to handle this coordination can be found in the Workbox documentation. (You can use this even if you're not using Workbox for your precaching.)

这篇关于新的更新可用消息和发布操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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