|
37 | 37 | <link rel="import" href="../bower_components/paper-button/paper-button.html"> |
38 | 38 | <link rel="import" href="../bower_components/paper-icon-button/paper-icon-button.html"> |
39 | 39 | <link rel="import" href="../bower_components/paper-styles/color.html"> |
| 40 | +<link rel="import" href="../bower_components/paper-toast/paper-toast.html"> |
40 | 41 |
|
41 | 42 | <link rel="import" href="experts-icons.html"> |
42 | 43 | <link rel="import" href="experts-signin.html"> |
@@ -368,6 +369,9 @@ <h1>Welcome!</h1> |
368 | 369 | user="{{user}}"></experts-not-logged-in> |
369 | 370 | </div> |
370 | 371 |
|
| 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 | + |
371 | 375 | <firebase-collection |
372 | 376 | location = "https://gooogle-experts-tracking-app.firebaseio.com/cache" |
373 | 377 | data = "{{cache}}"> |
@@ -420,9 +424,61 @@ <h1>Welcome!</h1> |
420 | 424 | _routePageChanged : function(page) { |
421 | 425 | this.page = page || 'activities'; |
422 | 426 | }, |
| 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 | + }, |
423 | 476 | _pageChanged : function(page) { |
| 477 | + |
| 478 | + var callback = this._ensureLazyLoaded.bind(this); |
424 | 479 | this.importHref( |
425 | | - this.resolveUrl('experts-' + page + '.html'), null, null, true); |
| 480 | + this.resolveUrl('experts-' + page + '.html'), callback, null, true); |
| 481 | + |
426 | 482 | }, |
427 | 483 | _userClicked: function() { |
428 | 484 | this._userSelected = !this._userSelected; |
|
0 commit comments