Cache API clear operation timing after Service Worker backup import in Chrome 80+
0 reputation · 21 Apr 2025, 11:08 UTC
0 reputation · 21 Apr 2025, 11:08 UTC
When restoring a previously exported Cache backup in a Service Worker, developers need to guarantee that the old cache is cleared before the imported data becomes available, preventing the serving of stale or partially updated assets.
However, the Cache API does not specify whether the clear() operation completes synchronously with respect to backup import events, leaving a potential race condition that could result in verification scripts observing incomplete restoration.
What guarantees exist about the ordering of clear() and import actions, how can developers reliably detect when the cache is ready for verification, and is there a recommended pattern to avoid serving stale content during this transition?
35275 reputation · 21 Apr 2025, 21:44 UTC
When a Service Worker imports a backup, Chrome uses a temporary in‑memory cache that is merged into the persistent CacheStorage once the worker is fully activated. The Cache.clear() method returns a Promise that resolves only after all queued delete operations have finished and the underlying IndexedDB transaction commits.
import() API is asynchronous and resolves only after the merge into the persistent store is complete.skipWaiting() and clients.claim(), the import may be deferred until the new worker takes control, adding extra latency.import() from the Service Worker and wait for its Promise to resolve. This guarantees that the temporary cache has been merged.clear(). Chain the Promises so that clear() is executed only after the merge is finished:
self.import('backup.json').then(() => {
return caches.open('app-cache');
}).then(cache => cache.clear());
cacheClear event occurs after the importCompleted event. The timestamps will show the clear promise resolving only after the merge.const t1 = performance.now();
self.import('backup.json').then(() => {
console.log('import done', performance.now() - t1);
return caches.open('app-cache');
}).then(cache => cache.clear()).then(() => {
console.log('clear done', performance.now() - t1);
});
To fine‑tune the timing, could you tell us the approximate size of the cache you are restoring? Large caches (>10 MB) can introduce longer IndexedDB write latencies.
Use comments to ask for clarification. Post a solution as an answer.
5,650 reputation · 21 Apr 2025, 19:59 UTC
The Cache API has no Cache.prototype.clear() or import() method. Bulk removal is caches.delete(cacheName) (returns a promise). Any clear() or import() you see is a non-standard helper or polyfill; behavior varies by Chrome version and library.
All Cache/CacheStorage operations settle asynchronously. There is no synchronous completion and no "import" event. To enforce order, await caches.delete(oldName), then caches.open(newName) and write entries. However, a concurrent fetch handler can still observe the old cache, an empty cache, or a partially populated one during the gap.
app-v42).await cache.match(sentinelRequest) or compare cache.keys() to your manifest.await caches.delete(oldName).This avoids serving stale or partial content during the transition.