GoogleChromeLabs / sw-precache

[Deprecated] A node module to generate service worker code that will precache specific resources so they work offline.
https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw
Apache License 2.0
5.22k stars 388 forks source link

How to use `registration.update()`? #285

Closed Shyam-Chen closed 7 years ago

Shyam-Chen commented 7 years ago
registration.update();
[...]
    .then(registration => {
      registration.onupdatefound = () => {
        if (navigator.serviceWorker.controller) {
          const { installing, update } = registration;

          installing.onstatechange = () => {
            switch (installing.state) {
              case 'installed':
                if (navigator.serviceWorker.controller) {
                  console.log('New or updated content is available.');
                  update();  // TypeError
                } else {
                  console.log('Content is now available offline!');
                }
                break;
              case 'redundant':
                throw new Error('The installing service worker became redundant.');
            }
          };
        }
      };
    })
[...]

Error Message:

Uncaught (in promise) TypeError: Failed to execute 'update' on 'ServiceWorkerRegistration': Illegal invocation
    at ServiceWorker.e.onstatechange (app-f925c92b44.js:2)