乐闻世界logo
搜索文章和话题

How to update service worker cache in PWA?

1个答案

1

When updating the cache for a Service Worker in a Progressive Web App (PWA), it is typically due to having new files to cache or wanting to update the versions of already cached files. The following outlines the steps to update the Service Worker cache and some recommended practices:

  1. Update the Service Worker File: First, modify the Service Worker JavaScript file. Typically, this is done by changing the cache name or updating specific content within the file, which prompts the browser to check for updates to the Service Worker file.

  2. Install the New Service Worker: When the browser detects an update to the Service Worker file, it attempts to install the new Service Worker. During this phase, you should add code to handle cache updates within the install event.

    javascript
    const CACHE_NAME = 'cache-v2'; // Change the cache name to create a new cache instance const urlsToCache = ['/', '/styles/main.css', '/script/main.js']; self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME) .then(cache => { return cache.addAll(urlsToCache); }) ); });
  3. Activate the New Service Worker: Next, the new Service Worker will trigger the activate event. During this phase, you typically clean up old caches to avoid excessive storage consumption.

    javascript
    self.addEventListener('activate', event => { const cacheWhitelist = [CACHE_NAME]; event.waitUntil( caches.keys().then(cacheNames => { return Promise.all( cacheNames.map(cacheName => { if (cacheWhitelist.indexOf(cacheName) === -1) { return caches.delete(cacheName); // Delete old caches not in the whitelist } }) )); }) ); });
  4. Update the Service Worker on the Client: After the new Service Worker is installed and activated, the page must be reloaded to utilize the updated Service Worker. You can add logic to your webpage to prompt users to reload or automatically refresh the page.

  5. Manage Updates: You can leverage self.skipWaiting() in the Service Worker to immediately activate a waiting Service Worker, or on the client side, use registration.waiting.postMessage('skipWaiting') to notify the Service Worker to update.

Real-World Example

Consider a scenario where a Service Worker cached several application resources in a previous version. Now, some resources have been updated, and we want to ensure users can access the latest versions. The following steps can be used to update the cache:

  1. Change the CACHE_NAME constant in the Service Worker file to create a new cache instance.
  2. Add the paths for new resources in the Service Worker's install event and ensure they are added to the new cache using the cache.addAll(urlsToCache) method.
  3. In the activate event, delete old cache versions by comparing the cacheWhitelist with the existing cache list.
  4. After the new Service Worker is activated, guide users to refresh the page or automatically refresh it to use the latest cached resources.

This process ensures that the application can promptly deliver the latest content to users while retaining offline usage capability.

2024年6月29日 12:07 回复

你的答案