如何使用Create React App实现skipWaiting? [英] How to implement skipWaiting with Create React App?

查看:68
本文介绍了如何使用Create React App实现skipWaiting?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够在有新服务人员可用时等待现场更新?当有新的更新可用时,我已经可以显示一个弹出窗口,但是我想添加一个按钮以当场强制更新。我知道可以通过调用skipWaiting来实现,但不确定如何使用Create React App来实现它。有谁能做到这一点?希望能有所帮助。谢谢!

I would like users to be able to update on the spot when a new service worker is available and waiting? I'm already able to show a pop up when new update is available but I would like to add a button to force update on the spot. I understand this can be achieved with calling skipWaiting but not sure how to implement it with a Create React App. Have anyone able to achieve this? Would appreciate the help. Thank you!

推荐答案

我使用了一个名为 https://github.com/bbhlondon/cra-append-sw 附加以下代码以调用触发器skipWaiting:

I used a package called https://github.com/bbhlondon/cra-append-sw to append the following code to call trigger skipWaiting:

self.addEventListener('message', event => {
  if (event.data === 'skipWaiting') {
    self.skipWaiting();
  }
});

这篇关于如何使用Create React App实现skipWaiting?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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