2016-11-30 50 views
2

我使用Polymer和polymer-cli以及爲緩存和脫機生成良好的服務人員構建了一個html/js應用程序(漸進式Web應用程序)。通過瀏覽器服務工作人員通知新的應用程序版本

我想知道如何在有新版本的應用程序可用時通知用戶並邀請他重新啓動瀏覽器。

有什麼想法?

編輯

在IO2016談話埃裏克Bidel談服務人員,並通知用戶有關應用程序的新版本: https://youtu.be/__KvYxcIIm8?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC&t=1510

需要檢查的谷歌IO網頁源代碼

+0

Web的應用程序的支持[推送通知(https://developers.google.com/網絡/基礎/工具入門/ codelabs /推通知/)。但這似乎是錯誤的通知用戶該應用程序已更新。你爲什麼要用戶「重啓瀏覽器」?服務工作人員會在檢測到任何更改時自動執行此操作... – alesc

+0

我看到並理解的是,服務工作人員不重新啓動應用程序,只是在緩存下載完成時更新自身和緩存,必須重新啓動在緩存中獲取新應用程序的應用程序...我只記得Eric在談論這個話題的時候,我會試着去找這個話題。 – eskan

回答

4

下面是引用: https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle

,這裏是什麼,我相信會回答你的問題

document.addEventListener('DOMContentLoaded', function(){ 
    if ('serviceWorker' in navigator) { 
     registerServiceWorker(); 
    } 

    function registerServiceWorker() { 
     navigator.serviceWorker.register('/service-worker.js').then(function(registration) { 
      checkForServiceWorkerUpdate(registration); 

      window.SWRegistrationObj = registration; 
      console.info('ServiceWorker registration successful with scope: ', registration.scope); 
     }, function(err) { 
      console.info('ServiceWorker registration failed: ', err); 
     }); 
    } 

    function checkForServiceWorkerUpdate(registration) { 
     registration.addEventListener('updatefound',() => { 
      console.log('show a toast'); 
      document.querySelector('#update-sw').addEventListener('click',() => { 
       window.location.reload(true); 
      }); 
     }); 
    } 
}); 
0

感謝IO團隊..我們需要檢查當前的服務人員是否變得冗餘

// Check to see if the service worker controlling the page at initial load 
// has become redundant, since this implies there's a new service worker with fresh content. 
if (navigator.serviceWorker && navigator.serviceWorker.controller) { 
    navigator.serviceWorker.controller.onstatechange = function(event) { 
    if (event.target.state === 'redundant') { 
     // Define a handler that will be used for the next io-toast tap, at which point it 
     // be automatically removed. 
     const tapHandler = function() { 
     window.location.reload(); 
     }; 

     if (IOWA.Elements && IOWA.Elements.Toast && 
      IOWA.Elements.Toast.showMessage) { 
      IOWA.Elements.Toast.showMessage(
      'A new version of this app is available.', tapHandler, 'Refresh', 
      null, 0); // duration 0 indications shows the toast indefinitely. 
     } else { 
     tapHandler(); // Force reload if user never was shown the toast. 
     } 
    } 
    }; 
} 
+0

我不認爲這是必要的,因爲'聚合物cli'在構建時產生服務工作者,我很確定這種代碼是包含的。至於引用IOWA應用程序...首選模板應用程序是[商店應用程序](https://github.com/Polymer/shop/),它不**包括'冗餘'狀態檢查。 – alesc

+0

我認爲商店應用程序不包含冗餘檢查,因爲它不是想要的行爲。大多數時間,服務人員用於緩存和離線:這是聚合物cli構建離線和緩存工作者的原因。但如果你關心你的用戶使用你的應用程序的最新版本,你需要處理它。如果瀏覽器在緩存更新後重新加載自身,那將會是一種糟糕的用戶體驗。 – eskan

相關問題