Setelah kita membahas instalasi Service Worker pada Progressive Web App, maka kita melangkah pada source code berikut berikutnya yang dibahas pada aplikasi pertama PWA. Source Code berikutnya adalah aktivasi Service Worker pada Progressive Web App.

Source code aktivasi Service Worker pada Progressive Web App adalah seperti berikut ini

self.addEventListener('activate', function(e) {
  console.log('[ServiceWorker] Activate');
  e.waitUntil(
    caches.keys().then(function(keyList) {
      return Promise.all(keyList.map(function(key) {
        if (key !== cacheName && key !== dataCacheName) {
          console.log('[ServiceWorker] Removing old cache', key);
          return caches.delete(key);
        }
      }));
    })
  );
  /*
   * Fixes a corner case in which the app wasn't returning the latest data.
   * You can reproduce the corner case by commenting out the line below and
   * then doing the following steps: 1) load app for first time so that the
   * initial New York City data is shown 2) press the refresh button on the
   * app 3) go offline 4) reload the app. You expect to see the newer NYC
   * data, but you actually see the initial data. This happens because the
   * service worker is not yet activated. The code below essentially lets
   * you activate the service worker faster.
   */
  return self.clients.claim();
});

Event activate ini akan dijalankan pada saat Service Worker mulai bekerja. Pada source code ini terlihat bahwa saat ada file dari app shell yang berubah maka cache juga akan terupdate. Pada source code ini juga ditampilkan penggunaan Promise pada PWA (Progressive Web App).

Informasi lebih lanjut silahkan mengunjungi
1. https://developers.google.com/web/fundamentals/codelabs/your-first-pwapp/
2. https://developers.google.com/web/progressive-web-apps/ .
3. Progressive Web Apps Training di https://developers.google.com/web/ilt/pwa/ .
4. https://developers.google.com/web/fundamentals/primers/service-workers/ .

Kunjungi www.proweb.co.id untuk menambah wawasan anda.

Aktivasi Service Worker dari PWA
× Ada yang dapat saya bantu ? Available on SundayMondayTuesdayWednesdayThursdayFridaySaturday