* Process a fetch event and return a `Response` if the resource is covered by this group, * or `null` otherwise.
(req: Request, event: ExtendableEvent)
| 301 | * or `null` otherwise. |
| 302 | */ |
| 303 | async handleFetch(req: Request, event: ExtendableEvent): Promise<Response | null> { |
| 304 | // Do nothing |
| 305 | if (!this.patterns.some((pattern) => pattern.test(req.url))) { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | // Lazily initialize the LRU cache. |
| 310 | const lru = await this.lru(); |
| 311 | |
| 312 | // The URL matches this cache. First, check whether this is a mutating request or not. |
| 313 | switch (req.method) { |
| 314 | case 'OPTIONS': |
| 315 | // Don't try to cache this - it's non-mutating, but is part of a mutating request. |
| 316 | // Most likely SWs don't even see this, but this guard is here just in case. |
| 317 | return null; |
| 318 | case 'GET': |
| 319 | case 'HEAD': |
| 320 | // Handle the request with whatever strategy was selected. |
| 321 | switch (this.config.strategy) { |
| 322 | case 'freshness': |
| 323 | return this.handleFetchWithFreshness(req, event, lru); |
| 324 | case 'performance': |
| 325 | return this.handleFetchWithPerformance(req, event, lru); |
| 326 | default: |
| 327 | throw new Error(`Unknown strategy: ${this.config.strategy}`); |
| 328 | } |
| 329 | default: |
| 330 | // This was a mutating request. Assume the cache for this URL is no longer valid. |
| 331 | const wasCached = lru.remove(req.url); |
| 332 | |
| 333 | // If there was a cached entry, remove it. |
| 334 | if (wasCached) { |
| 335 | await this.clearCacheForUrl(req.url); |
| 336 | } |
| 337 | |
| 338 | // Sync the LRU chain to non-volatile storage. |
| 339 | await this.syncLru(); |
| 340 | |
| 341 | // Finally, fall back on the network. |
| 342 | return this.safeFetch(req); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | private async handleFetchWithPerformance( |
| 347 | req: Request, |
nothing calls this directly
no test coverage detected