Skip to content

Instantly share code, notes, and snippets.

@stephenajulu
Forked from deanhume/offline-notification.js
Created April 22, 2020 09:44
Show Gist options
  • Save stephenajulu/128d96b186b9dd2531eaaecb187b9072 to your computer and use it in GitHub Desktop.
Save stephenajulu/128d96b186b9dd2531eaaecb187b9072 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