Skip to content
This repository was archived by the owner on Aug 14, 2018. It is now read-only.

Commit 7c02a91

Browse files
committed
Move SW to lazy load
1 parent 5468331 commit 7c02a91

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

index.html

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@
4949
document.head.appendChild(e);
5050
}
5151
})();
52-
// load pre-caching service worker
53-
if ('serviceWorker' in navigator) {
54-
window.addEventListener('load', function() {
55-
navigator.serviceWorker.register('/service-worker.js')
56-
.then(function(registration) {
57-
// Registration was successful
58-
console.log('ServiceWorker registration successful with scope: ', registration.scope);
59-
}).catch(function(err) {
60-
// registration failed :(
61-
console.log('ServiceWorker registration failed: ', err);
62-
});
63-
});
64-
}
6552
</script>
6653
<link rel="import" href="/src/experts-app.html">
6754
<style>

src/experts-activities/experts-activity-edit.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ <h2>
358358
type: Boolean,
359359
value: false
360360
},
361-
_activityTypesActive: {
362-
type: Boolean,
363-
computed: '_isActivityTypesActive(_activityTypes)'
364-
},
365361
},
366362
behaviors: [
367363
Polymer.PaperDialogBehavior

src/experts-app.html

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<link rel="import" href="../bower_components/paper-button/paper-button.html">
3838
<link rel="import" href="../bower_components/paper-icon-button/paper-icon-button.html">
3939
<link rel="import" href="../bower_components/paper-styles/color.html">
40+
<link rel="import" href="../bower_components/paper-toast/paper-toast.html">
4041

4142
<link rel="import" href="experts-icons.html">
4243
<link rel="import" href="experts-signin.html">
@@ -368,6 +369,9 @@ <h1>Welcome!</h1>
368369
user="{{user}}"></experts-not-logged-in>
369370
</div>
370371

372+
<paper-toast id="swReady" text="Service Worker ready! #pushthewebforward"></paper-toast>
373+
<paper-toast id="swReload" text="Looks like there is a new version of the app! Refresh to get the latest version."></paper-toast>
374+
371375
<firebase-collection
372376
location = "https://gooogle-experts-tracking-app.firebaseio.com/cache"
373377
data = "{{cache}}">
@@ -420,9 +424,61 @@ <h1>Welcome!</h1>
420424
_routePageChanged : function(page) {
421425
this.page = page || 'activities';
422426
},
427+
_ensureLazyLoaded: function() {
428+
429+
if (!this.loadComplete) {
430+
// If you've looked at the shop demo, this isn't the same. This
431+
// alerts the user to SW state changes. Debatable user xp, but I
432+
// decided to add it.
433+
if ('serviceWorker' in navigator) {
434+
navigator.serviceWorker.register('/service-worker.js')
435+
.then(function(registration) {
436+
console.log("hey");
437+
registration.onupdatefound = function() {
438+
439+
var installingWorker = registration.installing;
440+
441+
installingWorker.onstatechange = function() {
442+
443+
switch (installingWorker.state) {
444+
case 'installed':
445+
if (navigator.serviceWorker.controller) {
446+
447+
this.$.swReload.open();
448+
449+
} else {
450+
451+
// Note, if you wanted this SW to kick at this
452+
// point, you should do a page reload ala
453+
// window.location.reload();
454+
// or some such.
455+
this.$.swReady.open();
456+
}
457+
break;
458+
case 'redundant':
459+
// Error! Bad! No!
460+
break;
461+
}
462+
463+
}.bind(this);
464+
465+
}.bind(this);
466+
467+
}.bind(this)).catch(function(e) {
468+
469+
console.error('SW error on install', e.toString());
470+
471+
}.bind(this));
472+
}
473+
this.loadComplete = true;
474+
}
475+
},
423476
_pageChanged : function(page) {
477+
478+
var callback = this._ensureLazyLoaded.bind(this);
424479
this.importHref(
425-
this.resolveUrl('experts-' + page + '.html'), null, null, true);
480+
this.resolveUrl('experts-' + page + '.html'), callback, null, true);
481+
426482
},
427483
_userClicked: function() {
428484
this._userSelected = !this._userSelected;

0 commit comments

Comments
 (0)