Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ServiceWorker cache only when offline

I'm trying to integrate service workers into my app, but I've found the service worker tries to retrieve cached content even when online, but I want it to prefer the network in these situations. How can I do this? Below is the code I have now, but I don't believe it is working. SW Install code is omitted for brevity.

var CACHE_NAME = 'my-cache-v1'; var urlsToCache = [   /* my cached file list */ ];  self.addEventListener('install', function(event) {   // Perform install steps   event.waitUntil(     caches.open(CACHE_NAME)       .then(function(cache) {         console.log('Opened cache');         return cache.addAll(urlsToCache);       })   ); });  /* request is being made */ self.addEventListener('fetch', function(event) {   event.respondWith(     //first try to run the request normally     fetch(event.request).catch(function() {       //catch errors by attempting to match in cache       return caches.match(event.request).then(function(response) {         // Cache hit - return response         if (response) {           return response;         }       });     })   ); }); 

This seems to lead to warnings like The FetchEvent for "[url]" resulted in a network error response: an object that was not a Response was passed to respondWith(). I'm new to service workers, so apologies for any mistaken terminology or bad practices, would welcome any tips. Thank you!

like image 838
Ruben Martinez Jr. Avatar asked Oct 09 '15 19:10

Ruben Martinez Jr.


People also ask

Can PWAs work offline?

Our app is caching its resources on install and serving them with fetch from the cache, so it works even if the user is offline.

When should a Serviceworker be used?

Service workers are a fundamental part of a PWA. They enable fast loading (regardless of the network), offline access, push notifications, and other capabilities. Users expect apps to start on slow or flaky network connections, or even when offline.

Can service workers access cache?

Using a Service worker you can easily set an app up to use cached assets first, thus providing a default experience even when offline, before then getting more data from the network (commonly known as Offline First).

Is service worker necessary for PWA?

In my experience service workers aren't mandatory to deliver a PWA with offline functionality. As long as your PWA pages are properly marked as cacheable you can run your PWA completely offline and it won't need a connection to your server if the lifetime of the page is in the browsers cache lifetime.


1 Answers

Without testing this out, my guess is that you're not resolving respondWith() correctly in the case where there is no cache match. According to MDN, the code passed to respondWith() is supposed to "resolve by returning a Response or network error to Fetch." So why not try just doing this:

self.addEventListener('fetch', function(event) {   event.respondWith(     fetch(event.request).catch(function() {       return caches.match(event.request);     })   ); }); 
like image 151
Brendan Ritchie Avatar answered Sep 28 '22 02:09

Brendan Ritchie