Skip to content

Instantly share code, notes, and snippets.

@thesparrow
Created February 17, 2018 00:51
Show Gist options
  • Select an option

  • Save thesparrow/d2253641ec72b0a0618c15ca999d35d0 to your computer and use it in GitHub Desktop.

Select an option

Save thesparrow/d2253641ec72b0a0618c15ca999d35d0 to your computer and use it in GitHub Desktop.

Revisions

  1. thesparrow created this gist Feb 17, 2018.
    32 changes: 32 additions & 0 deletions notify-user-of-update.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    //Offline Web Applications course on Udacity : Grow with Google Application Course
    //code is placed in the controller file - not in the service worker itself
    IndexController.prototype._registerServiceWorker = function () {
    if (!navigator.serviceWorker) return; //feature detection

    var indexController = this;

    navigator.serviceWorker.register('/sw.js').then(function (reg) {

    //if no controller found - latest version is already loaded
    if (!navigator.serviceWorker.controller) return;

    // If there's an updated worker already waiting
    if (reg.waiting) {
    indexController._updateReady();
    return;
    }

    // If there's an updated worker installing, track its progress. If it becomes "installed"
    if (reg.installed) {
    indexController._updateReady();
    return;
    }

    //otherwise, listen for new installing workers arriving.
    // If one arrives, track its progress.
    reg.addEventListener('updateFound', function () {
    indexController._trackInstalling(reg.installing);
    });

    });
    };