javascript - 当应用程序已加载时,PWA 更新 Service Worker

标签 javascript updates service-worker progressive-web-apps

我正在构建一个 PWA,并且正在寻找一种在应用程序已打开并加载时更新 Service Worker 的方法。

例如,用户在手机上打开一次应用,然后在白天切换其他应用,而无需完全关闭 PWA。

如果我在中间上传了新版本,Service Worker 如何更新?

还有可能有一种方法可以在用户使用应用程序时通知服务有关更新吗?

Service Worker 是否能够不时自动检查是否上传了新内容?整个应用程序通过 s3 存储桶进行缓存和提供。

如有任何建议,我们将不胜感激。

最佳答案

来自 MDN "ServiceWorkerRegistration.update() " .

The update method of the ServiceWorkerRegistration interface attempts to update the service worker. It fetches the worker's script URL, and if the new worker is not byte-by-byte identical to the current worker, it installs the new worker. The fetch of the worker bypasses any browser caches if the previous fetch occurred over 24 hours ago.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
    // registration worked
    console.log('Registration succeeded.');
    button.onclick = function() {
      registration.update();
    }
  }).catch(function(error) {
    // registration failed
    console.log('Registration failed with ' + error);
  });
};

关于javascript - 当应用程序已加载时,PWA 更新 Service Worker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57403095/

相关文章:

ios - 当应用程序进入后台时,AVPlayer和addPeriodicTimeObserverForInterval不起作用

python - 更新 Windows 上的 scrapy

javascript - 从 1.2.2 升级 Ember Web 应用程序

java - 使用 facebooklogin 插件的网站并将用户凭据存储到数据库 JAVA 或 Javascript 到我表单的文本框中

javascript - 基于页面标题/位置的重定向

javascript - 如何在 React Native 中进行条件导航?

javascript - 在 client 和 serviceWorker 之间传输数据

javascript - 如何在 Greasemonkey 4 中准备好在 DOM 上运行代码?

reactjs - 使用 create-react-app 添加更多 service-worker 功能

reactjs - 使用 Service Worker 和推送通知使用react