You could easily pull the client's internet status by using Navigator.onLine
which returns a boolean.
if (!navigator.onLine) {
//Browser is offline, pull data from localStorage
} else {
//Browser is online, pull data through AJAX
}
You could also bind it to an event listener, so that you can change the data source on the fly:
window.addEventListener('offline', function(e) {
//Browser is offline, pull data from localStorage
});
Reference:
https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
At the time of posting, this function is supported on IE11, IE Edge, Firefox 51+ Chrome 49+, Safari 10+, iOS 9.3+, Android Browser 4.4+ and Chrome for Android 57+
Update It appears that I misunderstood your original question. After checking your manifest file. The code to pull the data from localStorage should go into the FALLBACK
section. You should place your localStorage function into the offline.html
file.
FALLBACK:
/ offline.html
Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Using_the_application_cache