(event: FetchEvent)
| 577 | } |
| 578 | |
| 579 | private async handleFetch(event: FetchEvent): Promise<Response> { |
| 580 | try { |
| 581 | // Ensure the SW instance has been initialized. |
| 582 | await this.ensureInitialized(event); |
| 583 | } catch { |
| 584 | // Since the SW is already committed to responding to the currently active request, |
| 585 | // respond with a network fetch. |
| 586 | return this.safeFetch(event.request); |
| 587 | } |
| 588 | |
| 589 | // On navigation requests, check for new updates. |
| 590 | if (event.request.mode === 'navigate' && !this.scheduledNavUpdateCheck) { |
| 591 | this.scheduledNavUpdateCheck = true; |
| 592 | this.idle.schedule('check-updates-on-navigation', async () => { |
| 593 | this.scheduledNavUpdateCheck = false; |
| 594 | await this.checkForUpdate(); |
| 595 | }); |
| 596 | } |
| 597 | |
| 598 | // Decide which version of the app to use to serve this request. This is asynchronous as in |
| 599 | // some cases, a record will need to be written to disk about the assignment that is made. |
| 600 | const appVersion = await this.assignVersion(event); |
| 601 | // If there's a configured max age, check whether this version is within that age. |
| 602 | const isVersionWithinMaxAge = |
| 603 | appVersion?.manifest.applicationMaxAge === undefined || |
| 604 | this.adapter.time - appVersion.manifest.timestamp < appVersion.manifest.applicationMaxAge; |
| 605 | let res: Response | null = null; |
| 606 | |
| 607 | try { |
| 608 | if (appVersion !== null && isVersionWithinMaxAge) { |
| 609 | try { |
| 610 | // Handle the request. First try the AppVersion. If that doesn't work, fall back on the |
| 611 | // network. |
| 612 | res = await appVersion.handleFetch(event.request, event); |
| 613 | } catch (err: any) { |
| 614 | if (err.isUnrecoverableState) { |
| 615 | await this.notifyClientsAboutUnrecoverableState(appVersion, err.message); |
| 616 | } |
| 617 | if (err.isCritical) { |
| 618 | // Something went wrong with handling the request from this version. |
| 619 | this.debugger.log(err, `Driver.handleFetch(version: ${appVersion.manifestHash})`); |
| 620 | await this.versionFailed(appVersion, err); |
| 621 | return this.safeFetch(event.request); |
| 622 | } |
| 623 | throw err; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | // The response will be `null` only if no `AppVersion` can be assigned to the request or if |
| 628 | // the assigned `AppVersion`'s manifest doesn't specify what to do about the request. |
| 629 | // In that case, just fall back on the network. |
| 630 | if (res === null) { |
| 631 | return this.safeFetch(event.request); |
| 632 | } |
| 633 | |
| 634 | // The `AppVersion` returned a usable response, so return it. |
| 635 | return res; |
| 636 | } finally { |
no test coverage detected