Skip to content

Instantly share code, notes, and snippets.

@deanhume
Created August 2, 2016 09:17
Show Gist options
  • Save deanhume/754f2bbd6e664f24db0c53a9e3902b35 to your computer and use it in GitHub Desktop.
Save deanhume/754f2bbd6e664f24db0c53a9e3902b35 to your computer and use it in GitHub Desktop.
Offline toast notifications
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