@@ -16,7 +16,7 @@ const searchParams = new URL(location.toString()).searchParams;
1616
1717const remoteAuthority = searchParams . get ( 'remoteAuthority' ) ;
1818
19- let outerIframeMessagePort : MessagePort | undefined ;
19+ const ID = searchParams . get ( 'id' ) ;
2020
2121/**
2222 * Origin used for resources
@@ -103,7 +103,6 @@ sw.addEventListener('message', async (event: ExtendableMessageEvent) => {
103103 const source = event . source as Client ;
104104 switch ( event . data . channel ) {
105105 case 'version' : {
106- outerIframeMessagePort = event . ports [ 0 ] ;
107106 sw . clients . get ( source . id ) . then ( client => {
108107 if ( client ) {
109108 client . postMessage ( {
@@ -203,24 +202,21 @@ async function processResourceRequest(
203202 event : FetchEvent ,
204203 requestUrlComponents : ResourceRequestUrlComponents
205204) : Promise < Response > {
206- let client = await sw . clients . get ( event . clientId ) ;
205+ const client = await sw . clients . get ( event . clientId ) ;
206+ let webviewId : string | null | undefined ;
207207 if ( ! client ) {
208- client = await getWorkerClientForId ( event . clientId ) ;
209- if ( ! client ) {
208+ const workerClient = await getWorkerClientForId ( event . clientId ) ;
209+ if ( ! workerClient ) {
210210 console . error ( 'Could not find inner client for request' ) ;
211211 return notFound ( ) ;
212+ } else {
213+ webviewId = getWebviewIdForClient ( workerClient ) ;
212214 }
215+ } else {
216+ webviewId = getWebviewIdForClient ( client ) ;
213217 }
214218
215- const webviewId = getWebviewIdForClient ( client ) ;
216-
217- // Refs https://github.com/microsoft/vscode/issues/244143
218- // With PlzDedicatedWorker, worker subresources and blob wokers
219- // will use clients different from the window client.
220- // Since we cannot different a worker main resource from a worker subresource
221- // we will use message channel to the outer iframe provided at the time
222- // of service worker controller version initialization.
223- if ( ! webviewId && client . type !== 'worker' && client . type !== 'sharedworker' ) {
219+ if ( ! webviewId ) {
224220 console . error ( 'Could not resolve webview id' ) ;
225221 return notFound ( ) ;
226222 }
@@ -325,6 +321,12 @@ async function processResourceRequest(
325321 return response . clone ( ) ;
326322 } ;
327323
324+ const parentClients = await getOuterIframeClient ( webviewId ) ;
325+ if ( ! parentClients . length ) {
326+ console . log ( 'Could not find parent client for request' ) ;
327+ return notFound ( ) ;
328+ }
329+
328330 let cached : Response | undefined ;
329331 if ( shouldTryCaching ) {
330332 const cache = await caches . open ( resourceCacheName ) ;
@@ -333,26 +335,8 @@ async function processResourceRequest(
333335
334336 const { requestId, promise } = resourceRequestStore . create ( ) ;
335337
336- if ( webviewId ) {
337- const parentClients = await getOuterIframeClient ( webviewId ) ;
338- if ( ! parentClients . length ) {
339- console . log ( 'Could not find parent client for request' ) ;
340- return notFound ( ) ;
341- }
342-
343- for ( const parentClient of parentClients ) {
344- parentClient . postMessage ( {
345- channel : 'load-resource' ,
346- id : requestId ,
347- scheme : requestUrlComponents . scheme ,
348- authority : requestUrlComponents . authority ,
349- path : requestUrlComponents . path ,
350- query : requestUrlComponents . query ,
351- ifNoneMatch : cached ?. headers . get ( 'ETag' ) ,
352- } ) ;
353- }
354- } else if ( client . type === 'worker' || client . type === 'sharedworker' ) {
355- outerIframeMessagePort ?. postMessage ( {
338+ for ( const parentClient of parentClients ) {
339+ parentClient . postMessage ( {
356340 channel : 'load-resource' ,
357341 id : requestId ,
358342 scheme : requestUrlComponents . scheme ,
@@ -377,13 +361,7 @@ async function processLocalhostRequest(
377361 return fetch ( event . request ) ;
378362 }
379363 const webviewId = getWebviewIdForClient ( client ) ;
380- // Refs https://github.com/microsoft/vscode/issues/244143
381- // With PlzDedicatedWorker, worker subresources and blob wokers
382- // will use clients different from the window client.
383- // Since we cannot different a worker main resource from a worker subresource
384- // we will use message channel to the outer iframe provided at the time
385- // of service worker controller version initialization.
386- if ( ! webviewId && client . type !== 'worker' && client . type !== 'sharedworker' ) {
364+ if ( ! webviewId ) {
387365 console . error ( 'Could not resolve webview id' ) ;
388366 return fetch ( event . request ) ;
389367 }
@@ -407,22 +385,15 @@ async function processLocalhostRequest(
407385 } ) ;
408386 } ;
409387
388+ const parentClients = await getOuterIframeClient ( webviewId ) ;
389+ if ( ! parentClients . length ) {
390+ console . log ( 'Could not find parent client for request' ) ;
391+ return notFound ( ) ;
392+ }
393+
410394 const { requestId, promise } = localhostRequestStore . create ( ) ;
411- if ( webviewId ) {
412- const parentClients = await getOuterIframeClient ( webviewId ) ;
413- if ( ! parentClients . length ) {
414- console . log ( 'Could not find parent client for request' ) ;
415- return notFound ( ) ;
416- }
417- for ( const parentClient of parentClients ) {
418- parentClient . postMessage ( {
419- channel : 'load-localhost' ,
420- origin : origin ,
421- id : requestId ,
422- } ) ;
423- }
424- } else if ( client . type === 'worker' || client . type === 'sharedworker' ) {
425- outerIframeMessagePort ?. postMessage ( {
395+ for ( const parentClient of parentClients ) {
396+ parentClient . postMessage ( {
426397 channel : 'load-localhost' ,
427398 origin : origin ,
428399 id : requestId ,
@@ -433,6 +404,15 @@ async function processLocalhostRequest(
433404}
434405
435406function getWebviewIdForClient ( client : Client ) : string | null {
407+ // Refs https://github.com/microsoft/vscode/issues/244143
408+ // With PlzDedicatedWorker, worker subresources and blob wokers
409+ // will use clients different from the window client.
410+ // Since we cannot different a worker main resource from a worker subresource
411+ // we will use the global webview ID passed in at the time of
412+ // service worker registration.
413+ if ( client . type === 'worker' || client . type === 'sharedworker' ) {
414+ return ID ;
415+ }
436416 const requesterClientUrl = new URL ( client . url ) ;
437417 return requesterClientUrl . searchParams . get ( 'id' ) ;
438418}
0 commit comments