|
9 | 9 | import {Adapter, Context} from './adapter'; |
10 | 10 | import {CacheState, UpdateCacheStatus, UpdateSource, UrlMetadata} from './api'; |
11 | 11 | import {Database, Table} from './database'; |
12 | | -import {SwCriticalError} from './error'; |
| 12 | +import {SwCriticalError, errorToString} from './error'; |
13 | 13 | import {IdleScheduler} from './idle'; |
14 | 14 | import {AssetGroupConfig} from './manifest'; |
15 | 15 | import {sha1Binary} from './sha1'; |
@@ -317,24 +317,33 @@ export abstract class AssetGroup { |
317 | 317 | `Response not Ok (fetchAndCacheOnce): request for ${req.url} returned response ${res.status} ${res.statusText}`); |
318 | 318 | } |
319 | 319 |
|
320 | | - // This response is safe to cache (as long as it's cloned). Wait until the cache operation |
321 | | - // is complete. |
322 | | - const cache = await this.scope.caches.open(`${this.prefix}:${this.config.name}:cache`); |
323 | | - await cache.put(req, res.clone()); |
324 | | - |
325 | | - // If the request is not hashed, update its metadata, especially the timestamp. This is needed |
326 | | - // for future determination of whether this cached response is stale or not. |
327 | | - if (!this.hashes.has(req.url)) { |
328 | | - // Metadata is tracked for requests that are unhashed. |
329 | | - const meta: UrlMetadata = {ts: this.adapter.time, used}; |
330 | | - const metaTable = await this.metadata; |
331 | | - await metaTable.write(req.url, meta); |
332 | | - } |
| 320 | + try { |
| 321 | + // This response is safe to cache (as long as it's cloned). Wait until the cache operation |
| 322 | + // is complete. |
| 323 | + const cache = await this.scope.caches.open(`${this.prefix}:${this.config.name}:cache`); |
| 324 | + await cache.put(req, res.clone()); |
| 325 | + |
| 326 | + // If the request is not hashed, update its metadata, especially the timestamp. This is |
| 327 | + // needed for future determination of whether this cached response is stale or not. |
| 328 | + if (!this.hashes.has(req.url)) { |
| 329 | + // Metadata is tracked for requests that are unhashed. |
| 330 | + const meta: UrlMetadata = {ts: this.adapter.time, used}; |
| 331 | + const metaTable = await this.metadata; |
| 332 | + await metaTable.write(req.url, meta); |
| 333 | + } |
333 | 334 |
|
334 | | - return res; |
| 335 | + return res; |
| 336 | + } catch (err) { |
| 337 | + // Among other cases, this can happen when the user clears all data through the DevTools, |
| 338 | + // but the SW is still running and serving another tab. In that case, trying to write to the |
| 339 | + // caches throws an `Entry was not found` error. |
| 340 | + // If this happens the SW can no longer work correctly. This situation is unrecoverable. |
| 341 | + throw new SwCriticalError( |
| 342 | + `Failed to update the caches for request to '${req.url}' (fetchAndCacheOnce): ${errorToString(err)}`); |
| 343 | + } |
335 | 344 | } finally { |
336 | 345 | // Finally, it can be removed from `inFlightRequests`. This might result in a double-remove |
337 | | - // if some other chain was already making this request too, but that won't hurt anything. |
| 346 | + // if some other chain was already making this request too, but that won't hurt anything. |
338 | 347 | this.inFlightRequests.delete(req.url); |
339 | 348 | } |
340 | 349 | } |
|
0 commit comments