Created
August 2, 2016 09:17
-
-
Save deanhume/754f2bbd6e664f24db0c53a9e3902b35 to your computer and use it in GitHub Desktop.
Offline toast notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function showOfflineNotification() { | |
if ('serviceWorker' in navigator) { | |
// Open the cache and check if we have this page saved | |
caches.open('beer-data').then(function(cache) { | |
var urlToCheck = 'https://deanhume.github.io/beer' + createStyleUrl(styleId, pageId, false); | |
cache.match(urlToCheck) | |
.then(function(response) { | |
if (response == undefined){ return; } // return if we found nothing | |
// We found the resource in cache | |
if (response.ok && localStorage.getItem(urlToCheck) === null) { | |
var snackbarContainer = document.querySelector('#offline-notification'); | |
var data = {message: 'This page is now available offline'}; | |
snackbarContainer.MaterialSnackbar.showSnackbar(data); | |
// Save the message into storage | |
localStorage.setItem(urlToCheck, true); | |
} | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment