dnk
1(function() {2'use strict';3
4// Check to make sure service workers are supported in the current browser,5// and that the current page is accessed from a secure origin. Using a6// service worker from an insecure origin will trigger JS console errors.7var isLocalhost = Boolean(window.location.hostname === 'localhost' ||8// [::1] is the IPv6 localhost address.9window.location.hostname === '[::1]' ||10// 127.0.0.1/8 is considered localhost for IPv4.11window.location.hostname.match(12/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/13)14);15
16window.addEventListener('load', function() {17if ('serviceWorker' in navigator &&18(window.location.protocol === 'https:' || isLocalhost)) {19navigator.serviceWorker.register('service-worker.js')20.then(function(registration) {21// updatefound is fired if service-worker.js changes.22registration.onupdatefound = function() {23// updatefound is also fired the very first time the SW is installed,24// and there's no need to prompt for a reload at that point.25// So check here to see if the page is already controlled,26// i.e. whether there's an existing service worker.27if (navigator.serviceWorker.controller) {28// The updatefound event implies that registration.installing is set29var installingWorker = registration.installing;30
31installingWorker.onstatechange = function() {32switch (installingWorker.state) {33case 'installed':34// At this point, the old content will have been purged and the35// fresh content will have been added to the cache.36// It's the perfect time to display a "New content is37// available; please refresh." message in the page's interface.38break;39
40case 'redundant':41throw new Error('The installing ' +42'service worker became redundant.');43
44default:45// Ignore46}47};48}49};50}).catch(function(e) {51console.error('Error during service worker registration:', e);52});53}54});55})();56