From 7958bb8f453addb7fd9ec11dbca858c657f8b52e Mon Sep 17 00:00:00 2001 From: Matthew Schile Date: Wed, 10 Apr 2024 11:27:28 -0600 Subject: [PATCH] handling document requests [run ci] --- .../lib/http/util/service-worker-manager.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/proxy/lib/http/util/service-worker-manager.ts b/packages/proxy/lib/http/util/service-worker-manager.ts index d6db3d593df2..fdc9fa7b33ca 100644 --- a/packages/proxy/lib/http/util/service-worker-manager.ts +++ b/packages/proxy/lib/http/util/service-worker-manager.ts @@ -168,6 +168,8 @@ export class ServiceWorkerManager { */ async processBrowserPreRequest (browserPreRequest: BrowserPreRequest) { if (browserPreRequest.initiator?.type === 'preload') { + debug('skipping preload request: %o', browserPreRequest) + return false } @@ -179,6 +181,18 @@ export class ServiceWorkerManager { activatedServiceWorker = registration.activatedServiceWorker const paramlessDocumentURL = browserPreRequest.documentURL?.split('?')[0] || '' + // if the service worker is active and the request is for the document URL, + // we can assume that the service worker is controlling the document + // and update the registration to reflect that + if (!registration.isHandlingRequests && + activatedServiceWorker?.initiatorOrigin && + browserPreRequest.documentURL === browserPreRequest.url && + browserPreRequest.url.startsWith(registration.scopeURL) && + browserPreRequest.documentURL.startsWith(activatedServiceWorker.initiatorOrigin)) { + registration.isHandlingRequests = true + debug('received request for the document of an activated service worker, updating registration to handle requests: %o', { registration, browserPreRequest }) + } + // We are determining here if a request is controlled by a service worker. A request is controlled by a service worker if // we have an activated service worker, the request URL does not come from the service worker, and the request // originates from the same origin as the service worker or from a script that is also controlled by the service worker. @@ -285,8 +299,8 @@ export class ServiceWorkerManager { const registration = this.getRegistrationForScope(scope) if (registration) { - debug('service worker is handling fetch requests: %o', registration) registration.isHandlingRequests = true + debug('service worker is handling fetch requests: %o', registration) } else { debug('could not find service worker registration for scope: %s', scope) }