【问题标题】:Is there a way to tell in the page that a service worker has recently upgraded?有没有办法在页面中告诉服务人员最近升级了?
【发布时间】:2020-06-22 17:19:57
【问题描述】:

我有一个带有服务人员的 SPA。

我不想强迫用户更新,所以通常新的工作人员在后台更新,但我通知用户,他们可以单击按钮向工作人员发送消息skipWaiting。发生这种情况时,我依靠 oncontrollerchange 强制刷新他们打开的任何其他选项卡。

我想在工作人员升级后显示发布说明的链接,无论是由于刷新所有选项卡还是他们强制刷新。

但是,我不想在他们第一次访问或每次服务人员激活时显示这些注释。如果他们不阅读发行说明,我不想继续唠叨他们。

是否有一个事件或可靠的设计模式可以用来告诉(在页面中)服务工作者已更新到新版本?

【问题讨论】:

    标签: javascript service-worker


    【解决方案1】:

    一个简单的解决方案是向当前 URL 添加一个查询参数,而不是调用 location.reload() 来重新加载您的页面,而是将 location 值更改为该 URL。

    类似:

    // Inside your `controllerchange` listener, call reloadWithNotes()
    function reloadWithNotes() {
      const url = new URL(location.href);
      url.searchParams.set('showNotes', '');
      location = url.href;
    }
    
    // Elsewhere...
    window.addEventListener('load', () => {
      const url = new URL(location.href);
      if (url.searchParams.has('showNotes')) {
        // Show your release notes.
    
        // Remove the parameter.
        url.searchParams.delete('showNotes');
        window.history.replaceState({}, document.title, url.href);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2011-06-19
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多