Skip to content

Instantly share code, notes, and snippets.

@thesparrow
Created February 17, 2018 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesparrow/d2253641ec72b0a0618c15ca999d35d0 to your computer and use it in GitHub Desktop.
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
//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