Polymer app refresh strategy

Our app is a Polymer 2 single page app, where we have some custom build steps to generate versioned files of the resources (gulp-rev-all). All is working well, but we need to have implement a safe refresh of the application. What we are doing is that we keep latest installed git commit in a file that is served together with the application, and we pull for this file in intervals and alerts the user that there is a new version available and ask them to click on a button to refresh the application.

The problem is that we are using service worker with pre-cache as the default Polymer build provides. This means when we do location.reload() we actually get the content (index.html) from the service worker, and not from the server.

So the question is: how can we enforce that the service worker would be invalidated and force a new refresh of service-worker.js and index.html?


这里的准备好的代码,都描述了如何处理服务人员;

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('sw.js').then(function(reg) {
    // updatefound is fired if sw.js changes.
    reg.onupdatefound = function() {
    // The updatefound event implies that reg.installing is set; see
    // https://w3c.github.io/ServiceWorker/#service-worker-registration-updatefound-event
    var installingWorker = reg.installing;
    installingWorker.onstatechange = function() {
    switch (installingWorker.state) {
        case 'installed':
            if (navigator.serviceWorker.controller) {
            // At this point, the old content will have been purged and the fresh content will
            // have been added to the cache.
            // It's the perfect time to display a "New content is available; please refresh."
            // message in the page's interface.
            console.log('New or updated content is available.');
            // Need to hard refresh when new content is available
            location.reload(true);
            } else {
                // At this point, everything has been precached.
                // It's the perfect time to display a "Content is cached for offline use." message.
                console.log('Content is now available offline!');
                }
            break;
        case 'redundant':
            console.error('The installing service worker became redundant.');
            break;
        }
    };
    };

    }).catch(function(e) {
       console.error('Error during service worker registration:', e);
        });
     }
链接地址: http://www.djcxy.com/p/5342.html

上一篇: 将数据URI转换为文件,然后附加到FormData

下一篇: 聚合物应用刷新策略