0

i cached 2 web pages in different folders to make PWA, (they are linked pages) but when i worked offline it loaded the 1st page only. The coded pages, manifest and service worker are below:

Page A => this is the main app page:

<head>
<link rel="manifest" href="/Folder1/manifest.json">
</head>
<body>
    <script>
        if('serviceWorker' in navigator){
            try{
                navigator.serviceWorker.register('sw.js').then(function(reg) {
                console.log("SW registered for scope: " + reg.scope);
                });
            } catch ( error ) {
                console.log('SW reg failed');
            }
        }
    </script>
    <a href="../Folder2/Page B">Page B</a><br>
</body>

Page B => this is a sub page in different folder but not the only one:

<head>
<link rel="manifest" href="../Folder1/manifest.json">
</head>
<body>
    <script>
        if('serviceWorker' in navigator){
            try{
                navigator.serviceWorker.register('../Folder1/sw.js');
                console.log('SW registered2');
            } catch ( error ) {
                console.log('SW reg failed2');
            }
        }
    </script>
    <h1>Content</h1>
</body>

Manifest File => located with Page A folder:

{
  "name": "Web Outline Work",
  "short_name": "WOW",
  "theme_color": "#2196f3",
  "background_color": "#2196f3",
  "display": "standalone",
  "scope": "/",
  "start_url": ".",
  "icons": [
    {
      "src": "images/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ],
  "splash_pages": null
}

what dose scope means and what should i add in it?

Service Worker File => located with manifest file

const cachedData = [
    './',
    '../Folder2/Page B'
];


self.addEventListener('install', async event => {
    const cache = await caches.open('app-static');
    cache.addAll(cachedData);
});

self.addEventListener('fetch', event => {
    const req = event.request;
    const url = new URL(req.url);
    if(url.origin === location.origin){
        event.respondWith(cacheFirest(req));
    } else {
        event.respondWith(networkFirest(req));
    }

});

async function cacheFirest(req){
    const cacheResp = await caches.match(req);
    return cacheResp || fetch(req);
}

async function networkFirest(req){
    const cache = await caches.open('app-dynamic');
    try{
        const res = await fetch(req);
        cache.put(req, res.clone());
        return res;
    } catch ( error ) {
        return await cache.match(req);
    }
}
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • Did you tried to go back with the scope ? `"scope": "../",` – bill.gates Jun 19 '20 at 13:48
  • @Ifaruki yes still cant load page b from cache in offline mood – David Raouf Jun 19 '20 at 19:16
  • I am developing an small library that should help people with building pwa. https://github.com/ilijanovic/serviceHelper/blob/master/README.md#how-to-make-my-app-installable. It has currently not that much features. It registers your SW, you can execute `installApp` method to show up a prompt and listen to `notinstalled` if its not installed. Features like subscribing to push nessages is comming soon, would be nice to rate it if you want – bill.gates Jun 19 '20 at 19:38
  • @ifaruki i will check it and is there a way to call back a cached page and open it offline from cache ? (sure other than the original page => page a) – David Raouf Jun 19 '20 at 20:04
  • @maxischl hi i read your comment regarding PWA https://stackoverflow.com/questions/56171281/pwa-offline-mode-not-loading-from-cache-on-mobile-browsers and i added the sw.js in my cache but still didnt open on mobile in offline mood can you help please? code is up there – David Raouf Jun 19 '20 at 22:53

1 Answers1

1

Your scope needs to include both folders. Check out this article

The scope attribute can be a relative path (../), or any higher level path (/) which would allow for an increase in coverage of navigations in your web app.

Hope this helps!

Pedro Filipe
  • 995
  • 1
  • 8
  • 17