Skip to content

Commit bc35632

Browse files
authored
Add more robust SW registration snippet (#13)
* Add more robust SW registration snippet * delay SW initial registration until after 1st page load
1 parent 72c64ee commit bc35632

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed

template/index.html

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,61 @@
2626
<div id="app">This is your fallback content in case JavaScript fails to load.</div>
2727
<!-- Todo: only include in production -->
2828
<script>
29-
if('serviceWorker' in navigator) {
30-
navigator.serviceWorker.register('/service-worker.js', { scope: '/' })
31-
.then(function(registration) {
32-
console.log('Service Worker Registered');
33-
});
29+
(function() {
30+
'use strict';
3431

35-
navigator.serviceWorker.ready.then(function(registration) {
36-
console.log('Service Worker Ready');
32+
// Check to make sure service workers are supported in the current browser,
33+
// and that the current page is accessed from a secure origin. Using a
34+
// service worker from an insecure origin will trigger JS console errors.
35+
const isLocalhost = Boolean(window.location.hostname === 'localhost' ||
36+
// [::1] is the IPv6 localhost address.
37+
window.location.hostname === '[::1]' ||
38+
// 127.0.0.1/8 is considered localhost for IPv4.
39+
window.location.hostname.match(
40+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
41+
)
42+
);
43+
44+
window.addEventListener('load', function() {
45+
if ('serviceWorker' in navigator &&
46+
(window.location.protocol === 'https:' || isLocalhost)) {
47+
navigator.serviceWorker.register('service-worker.js')
48+
.then(function(registration) {
49+
// updatefound is fired if service-worker.js changes.
50+
registration.onupdatefound = function() {
51+
// updatefound is also fired the very first time the SW is installed,
52+
// and there's no need to prompt for a reload at that point.
53+
// So check here to see if the page is already controlled,
54+
// i.e. whether there's an existing service worker.
55+
if (navigator.serviceWorker.controller) {
56+
// The updatefound event implies that registration.installing is set
57+
const installingWorker = registration.installing;
58+
59+
installingWorker.onstatechange = function() {
60+
switch (installingWorker.state) {
61+
case 'installed':
62+
// At this point, the old content will have been purged and the
63+
// fresh content will have been added to the cache.
64+
// It's the perfect time to display a "New content is
65+
// available; please refresh." message in the page's interface.
66+
break;
67+
68+
case 'redundant':
69+
throw new Error('The installing ' +
70+
'service worker became redundant.');
71+
72+
default:
73+
// Ignore
74+
}
75+
};
76+
}
77+
};
78+
}).catch(function(e) {
79+
console.error('Error during service worker registration:', e);
80+
});
81+
}
3782
});
38-
}
83+
})();
3984
</script>
4085
<!-- built files will be auto injected -->
4186
</body>

0 commit comments

Comments
 (0)