SuperPWA / super-progressive-web-apps

SuperPWA helps to convert your WordPress website into Progressive Web Apps instantly. PWA (Progressive Web Apps) demo at : https://superpwa.com and Plugin :
https://wordpress.org/plugins/super-progressive-web-apps/
GNU General Public License v2.0
386 stars 82 forks source link

Issue with Network first, then Cache #336

Closed Sdaud1950 closed 1 year ago

Sdaud1950 commented 2 years ago

While using the SuperPwa with Network first, then Cache, there is an error shown sometimes. Ref screenshot. https://prnt.sc/zxcjYLfdEjnU

Ref Ticket .https://magazine3.in/conversation/59013?folder_id=29

Ref code solution. https://developer.chrome.com/docs/workbox/caching-strategies-overview/#network-first-falling-back-to-cache https://prnt.sc/pfBPzSTzIeXB

dwise85 commented 2 years ago

For example, if there is a slow network, spotty network, server outage, etc. These various error states should cause fetch() API to throw an error. As seen in the references Google caching strategies the appropriate mitigation for those errors for a PWA is to catch the error and serve from the Cache as so:

return fetch(event.request.url).then((fetchedResponse) => {
  ...
}).catch(() => {
  // If the network is unavailable, get
  return cache.match(event.request.url);
});

This would be a slight adjustment to the existing logic of Super PWA from:

            if ( (e.request.mode === 'navigate' || e.request.mode === 'cors') && navigator.onLine ) {
                e.respondWith(
                    fetch(e.request).then(function(response) {
                        return caches.open(cacheName).then(function(cache) {
                            cache.put(e.request, response.clone());
                            return response;
                        });  
                    })
                );
                return;
            }

to

            if ( (e.request.mode === 'navigate' || e.request.mode === 'cors') && navigator.onLine ) {
                e.respondWith(
                    fetch(e.request).then(function(response) {
                        return caches.open(cacheName).then(function(cache) {
                            cache.put(e.request, response.clone());
                            return response;
                        });  
                    }).catch(() => {
                                          // If the network is unavailable, get
                                          return cache.match(event.request.url);
                                        });
                );
                return;
            }
akshaywali commented 1 year ago

update pushed in 2.2.12