Service worker add files from API call to precache

后端 未结 2 1501
迷失自我
迷失自我 2021-01-06 18:33

To enable my app running offline. During installation the service worker should:

  1. fetch a list of URLs from an async API
  2. reformat the response
  3. <
2条回答
  •  攒了一身酷
    2021-01-06 18:55

    I realised my mistake. I hope this helps others as well. The problem was that I did not call precacheController.install() manually. While this function will be executed automatically it will not wait for additional precache files that are inserted asynchronously. This is why the function needs to be called after all the precaching happened. Here is the working code:

    importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.1.0/workbox-sw.js');
    
    workbox.skipWaiting();
    workbox.clientsClaim();
    
    const precacheController = new workbox.precaching.PrecacheController();
    
    // Hook into install event
    self.addEventListener('install', (event) => {
      // Get API URL passed as query parameter to service worker
      const preInstallUrl = new URL(location).searchParams.get('preInstallUrl');
    
      // Fetch precaching URLs and attach them to the cache list
      const assetsLoaded = fetch(preInstallUrl)
        .then(response => response.json())
        .then((values) => {
          Object.keys(values.data.Assets).forEach((key) => {
            precacheController.addToCacheList([values.data.Assets[key]]);
          });
        })
        .then(() => {
          // After all assets are added install them
          precacheController.install();
        });
      event.waitUntil(assetsLoaded);
    });
    
    self.__precacheManifest = [].concat(self.__precacheManifest || []);
    workbox.precaching.suppressWarnings();
    workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
    
    workbox.routing.registerRoute(/^.*\.(jpg|JPG|gif|GIF|png|PNG|eot|woff(2)?|ttf|svg)$/, workbox.strategies.cacheFirst({ cacheName: 'image-cache', plugins: [new workbox.cacheableResponse.Plugin({ statuses: [0, 200] }), new workbox.expiration.Plugin({ maxEntries: 600 })] }), 'GET');
    

提交回复
热议问题