Created
February 17, 2018 00:51
-
-
Save thesparrow/d2253641ec72b0a0618c15ca999d35d0 to your computer and use it in GitHub Desktop.
This snippet shows to to notify user when updates are available using a service worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment