I want my Angularjs (1.x) application to have an offline version as fallback in case there is no network or no internet service.
I want the service worker's fetch event listener, try to fetch the main page from the network (when the request.mode == 'navigate') and return the a cached version if it fails.
For some reason, even though I've disconnected the wifi or run in "airplane mode" the fetch always returns StatusCode 200 OK. But it does work indeed, if I turn on Chrome's DevTools "Network > offline" mode
...What I've tried:
I've tried detecting offline mode with "navigator.onLine" feature, but is not reliable.
Also tried to clear cache, but nothing.. still returns the original "online" html document..
Also tried to pass a "cache-control: no-store, no-cache" header to the fetch, with same result..
self.addEventListener('fetch', function (event) {
if (event.request.mode === 'navigate') {
event.respondWith(homeNetworkThenCache(event.request));
}
});
function homeNetworkThenCache(request){
return fetch(request)
.then(e => e)
.catch(() => caches.match(offlineHomepageUrl));
}
Expected behavior:
If there's no internet connection, I expect the fetch to enter the "catch" block, but it always enters the ".then(e => e)"...
..any ideas, please?