0

I enabled PWA for my reactjs application and deployed on my domain (arvind.inzack.com)

As part of testing, we installed the app on Samsung Tab device. Once the application is loaded successfully we disabled the wifi connection and reloaded the browser. The content in the App is not getting loaded.

we enabled all the files in the cache using Serviceworker.js

let CACHE_NAME = 'my-site-cache-v1';

const urlsToCache = [
'/',
'/index.html',
];

const precacheadd1Resources = ['/', '/shirt-trouser/shirts/.jpeg', '/shirt-trouser/shirts/.JPG', '/shirt-trouser/shirts/.PNG', '/shirt-trouser/shirts/.png'];
const precacheadd2Resources = ['/', '/shirt-trouser/trouser_plain/.jpeg', '/shirt-trouser/trouser_plain/.JPG', '/shirt-trouser/trouser_plain/.PNG', '/shirt-trouser/trouser_plain/.png'];
const precacheadd3Resources = ['/', '/shirt-trouser/belt/.jpeg', '/shirt-trouser/belt/.JPG', '/shirt-trouser/belt/.PNG', '/shirt-trouser/belt/.png'];

const precacheadd4Resources = ['/config.js', '/index.js','App.js','components/BabylonAvatar.js','components/SceneComponent.js'];
const precacheadd5Resources = ['/avatar/newformalwalk13.glb'];

window.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache, 
    precacheadd1Resources,
    precacheadd2Resources,
    precacheadd3Resources,
    precacheadd4Resources,
    precacheadd5Resources);
})
);
});

Please suggest how can we using the PWA App in offline mode.

I am trying with sw-toolbox cache, Using this link am trying to integrate

can somebody share the approach how to integrate sw-toolbox with with reactjs

Thanks Asha

Asha Datla
  • 126
  • 1
  • 11

2 Answers2

0

I see that you cache the resources, but activate and fetch listeners are missing. The fetch listener is triggered on each network request to cache and serve cached assets.

This is how my sw.js looks like:

const CACHE_VERSION = 'v1.17';
const CACHES = [{
  id: 'offline',
  name: `offline_${CACHE_VERSION}`,
  urls: [
    '/offline',
    '/manifest.json',
    '/icon/favicon.ico',
    '/icon/android-chrome-192x192.png',
    '/build/images/logo_white.svg'
  ]
}, {
  id: 'assets',
  name: `assets_${CACHE_VERSION}`,
  urls: []
}];
const CACHE_NAMES = CACHES.map(cache => cache.name);

// Triggered when no SW is yet registered to precache static assets
self.addEventListener('install', event => {
  self.skipWaiting();

  event.waitUntil((async () => {
    for (const myCache of CACHES) {
      const cache = await caches.open(myCache.name);
      console.info('[Service Worker] Precaching resources:', myCache.urls);
      await cache.addAll(myCache.urls);
    }
  })());
});

// Triggered when SW version updated to delete old cache
self.addEventListener('activate', event => {
  event.waitUntil((async () => {
    const cacheNames = await caches.keys();

    for (const cacheName of cacheNames) {
      if (CACHE_NAMES.indexOf(cacheName) === -1)) {
        console.info(`[Service Worker] Deleting cache: ${cacheName}`);
        await caches.delete(cacheName);
      }
    }
  })());
});

// Triggered on each network request to cache and serve cached assets
self.addEventListener('fetch', event => {
  event.respondWith((async () => {
    const { url, destination, mode } = event.request;
    let match, response;

    try {
      match = await caches.match(event.request);
      // serve cached if exist
      if (match) return match;
      // fetch and serve from network
      response = await fetch(event.request);
    } catch (err) {
      if (mode === 'navigate') {
        return await caches.match('/offline');
      }
    }

    if (['font', 'image', 'style', 'script'].includes(destination)) {
      const cacheName = CACHES.find(cache => cache.id === 'assets').name;
      const cache = await caches.open(cacheName);
      console.info(`[Service Worker] Caching new resource: ${url}`);
      await cache.put(event.request, response.clone());
    }

    return response;
  })());
});

Notice how I precached all the necessary resources for the offline mode, now in fetch listener:

  • I try to get a cached resource;

  • if resource is not cached - I try to fetch it;

  • if there is no internet connection - I request cached /offline page;

     try {
       match = await caches.match(event.request);
       // serve cached if exist
       if (match) return match;
       // fetch and serve from network
       response = await fetch(event.request);
     } catch (err) {
       if (mode === 'navigate') {
         return await caches.match('/offline');
       }
     }
    
Ignas Damunskis
  • 1,515
  • 1
  • 17
  • 44
  • Thanks @Ignas , In sw.js Initially u declared const CACHE_SCOPE = registration.scope; i tried by const CACHE_SCOPE = navigator.serviceWorker.getRegistration.scope; it is not working if the app is invoked by IP address – Asha Datla Jun 30 '23 at 10:18
  • You don't need to access scope like me, I use it because I register separate sw for different website paths and I name caches based on scope to separate them. Ignore that part. You should only precache in install listener and serve cached resources in fetch listener. – Ignas Damunskis Jun 30 '23 at 13:48
  • Updated the code removing irrelevant code – Ignas Damunskis Jun 30 '23 at 13:53
  • Thanks @Ignas for your extended support, once i deployed the code i tested the application in internet by accessing the application so that all data is cached. Later on i disabled the internet and tried to access the web application in offline mode. The error am getting is CORS issue attached https://ibb.co/WcWLv8T Basically we are trying to store all the images/glb files in the cache and retrieve in offline mode. Can u help us pls.. – Asha Datla Jul 03 '23 at 10:15
  • Are you trying to fetch it from the fetch event listener? Could you share returned headers and response in the networks tab. – Ignas Damunskis Jul 04 '23 at 07:54
  • Thanks @Ignas Damunskis for yur help Pls find the error screenshot https://i.ibb.co/YDD8hBw/error.png https://i.ibb.co/5hr7Q95/error1.png – Asha Datla Jul 05 '23 at 00:34
0

You must add urls like this:

return cache.addAll([...urlsToCache, 
    ...precacheadd1Resources,
    ...precacheadd2Resources,
    ...precacheadd3Resources,
    ...precacheadd4Resources,
    ...precacheadd5Resources]);
})

You must add only one array in addAll method and like this you puth all urls in one array

QasemBSK
  • 190
  • 8
  • Thanks @QasemBSK, I have edited the code as per yur suggestion, At line 91 unable to trigger event listener "offline mode" in case of with out internet – Asha Datla Jul 04 '23 at 01:40