1

I was just wondering if there is a convenient way to cache an entire PWA on click? Just like you would download and install a native app from an app store?

If I am not wrong, the only solution currently is that you have to add all existing files in an array and use the cache.addAll method (as you see below). You can execute the function then if the button was clicked.

function downloadApp() {
  caches.open(appCache).then(function(cache) {
    return cache.addAll([
      '/',
      '/files/1',
      '/files/2',
      // ...
      // All PWA files
    ]);
  })
}

Do you know any better approaches for this?

kmuenster
  • 43
  • 1
  • 3
  • 1
    perhaps notify app via `postMessage` when `addAll` resolves and the app is cached? And for larger apps, a progress indicator? – anthumchris Feb 06 '20 at 15:49
  • But is there a better way than using the `addAll` method? If you want to download the entire app on click, you have to maintain an array of all file URLs which isn't very convenient. – kmuenster Feb 09 '20 at 10:42
  • Not that I'm immediately aware of. You could always dynamically pass a list of files to the SW to avoid static declarations in the SW – anthumchris Feb 09 '20 at 19:55
  • 1
    Alright, thanks anyway! I just saw that there is a way to download a zip file, decompress it in the service worker, and cache all individual files afterwards. See [here](https://serviceworke.rs/cache-from-zip.html). Thus, you can bundle your PWA and make it offline available with a single request. – kmuenster Feb 10 '20 at 16:07

0 Answers0