@@ -93,27 +93,16 @@ async function doFetch(event: FetchEvent): Promise<Response> {
93
93
const request = event . request ;
94
94
const client = await self . clients . get ( event . clientId ) ;
95
95
96
- const urlInScope = request . url . startsWith ( self . registration . scope ) ?new URL ( unwrapPopoutUrl ( request . url ) ) :undefined ;
97
- const relativePath = urlInScope ?. pathname . substring ( scopePath . length - 1 ) ;
98
-
99
- if ( relativePath !== '/contexts' && ! clientIdToTraceUrls . has ( event . clientId ) ) {
100
- // Service worker was restarted upon subresource fetch.
101
- // It was stopped because ping did not keep it alive since the tab itself was throttled.
102
- const params = await loadClientIdParams ( event . clientId ) ;
103
- if ( params ) {
104
- for ( const traceUrl of params . traceUrls )
105
- await loadTrace ( traceUrl , null , client , params . limit , ( ) => { } ) ;
106
- }
107
- }
108
-
109
96
// When trace viewer is deployed over https, we will force upgrade
110
97
// insecure http subresources to https. Otherwise, these will fail
111
98
// to load inside our https snapshots.
112
99
// In this case, we also match http resources from the archive by
113
100
// the https urls.
114
101
const isDeployedAsHttps = self . registration . scope . startsWith ( 'https://' ) ;
115
102
116
- if ( urlInScope && relativePath ) {
103
+ if ( request . url . startsWith ( self . registration . scope ) ) {
104
+ const url = new URL ( unwrapPopoutUrl ( request . url ) ) ;
105
+ const relativePath = url . pathname . substring ( scopePath . length - 1 ) ;
117
106
if ( relativePath === '/ping' ) {
118
107
await gc ( ) ;
119
108
return new Response ( null , { status :200 } ) ;
@@ -123,12 +112,12 @@ async function doFetch(event: FetchEvent): Promise<Response> {
123
112
return new Response ( null , { status :200 } ) ;
124
113
}
125
114
126
- const traceUrl = urlInScope . searchParams . get ( 'trace' ) ;
115
+ const traceUrl = url . searchParams . get ( 'trace' ) ;
127
116
128
117
if ( relativePath === '/contexts' ) {
129
118
try {
130
- const limit = urlInScope . searchParams . has ( 'limit' ) ?+ urlInScope . searchParams . get ( 'limit' ) ! :undefined ;
131
- const traceModel = await loadTrace ( traceUrl ! , urlInScope . searchParams . get ( 'traceFileName' ) , client , limit , ( done :number , total :number ) => {
119
+ const limit = url . searchParams . has ( 'limit' ) ?+ url . searchParams . get ( 'limit' ) ! :undefined ;
120
+ const traceModel = await loadTrace ( traceUrl ! , url . searchParams . get ( 'traceFileName' ) , client , limit , ( done :number , total :number ) => {
132
121
client . postMessage ( { method :'progress' , params :{ done, total} } ) ;
133
122
} ) ;
134
123
return new Response ( JSON . stringify ( traceModel ! . contextEntries ) , {
@@ -143,20 +132,30 @@ async function doFetch(event: FetchEvent): Promise<Response> {
143
132
}
144
133
}
145
134
135
+ if ( ! clientIdToTraceUrls . has ( event . clientId ) ) {
136
+ // Service worker was restarted upon subresource fetch.
137
+ // It was stopped because ping did not keep it alive since the tab itself was throttled.
138
+ const params = await loadClientIdParams ( event . clientId ) ;
139
+ if ( params ) {
140
+ for ( const traceUrl of params . traceUrls )
141
+ await loadTrace ( traceUrl , null , client , params . limit , ( ) => { } ) ;
142
+ }
143
+ }
144
+
146
145
if ( relativePath . startsWith ( '/snapshotInfo/' ) ) {
147
146
const { snapshotServer} = loadedTraces . get ( traceUrl ! ) || { } ;
148
147
if ( ! snapshotServer )
149
148
return new Response ( null , { status :404 } ) ;
150
149
const pageOrFrameId = relativePath . substring ( '/snapshotInfo/' . length ) ;
151
- return snapshotServer . serveSnapshotInfo ( pageOrFrameId , urlInScope . searchParams ) ;
150
+ return snapshotServer . serveSnapshotInfo ( pageOrFrameId , url . searchParams ) ;
152
151
}
153
152
154
153
if ( relativePath . startsWith ( '/snapshot/' ) ) {
155
154
const { snapshotServer} = loadedTraces . get ( traceUrl ! ) || { } ;
156
155
if ( ! snapshotServer )
157
156
return new Response ( null , { status :404 } ) ;
158
157
const pageOrFrameId = relativePath . substring ( '/snapshot/' . length ) ;
159
- const response = snapshotServer . serveSnapshot ( pageOrFrameId , urlInScope . searchParams , urlInScope . href ) ;
158
+ const response = snapshotServer . serveSnapshot ( pageOrFrameId , url . searchParams , url . href ) ;
160
159
if ( isDeployedAsHttps )
161
160
response . headers . set ( 'Content-Security-Policy' , 'upgrade-insecure-requests' ) ;
162
161
return response ;
@@ -167,7 +166,7 @@ async function doFetch(event: FetchEvent): Promise<Response> {
167
166
if ( ! snapshotServer )
168
167
return new Response ( null , { status :404 } ) ;
169
168
const pageOrFrameId = relativePath . substring ( '/closest-screenshot/' . length ) ;
170
- return snapshotServer . serveClosestScreenshot ( pageOrFrameId , urlInScope . searchParams ) ;
169
+ return snapshotServer . serveClosestScreenshot ( pageOrFrameId , url . searchParams ) ;
171
170
}
172
171
173
172
if ( relativePath . startsWith ( '/sha1/' ) ) {
@@ -176,13 +175,13 @@ async function doFetch(event: FetchEvent): Promise<Response> {
176
175
for ( const trace of loadedTraces . values ( ) ) {
177
176
const blob = await trace . traceModel . resourceForSha1 ( sha1 ) ;
178
177
if ( blob )
179
- return new Response ( blob , { status :200 , headers :downloadHeaders ( urlInScope . searchParams ) } ) ;
178
+ return new Response ( blob , { status :200 , headers :downloadHeaders ( url . searchParams ) } ) ;
180
179
}
181
180
return new Response ( null , { status :404 } ) ;
182
181
}
183
182
184
183
if ( relativePath . startsWith ( '/file/' ) ) {
185
- const path = urlInScope . searchParams . get ( 'path' ) ! ;
184
+ const path = url . searchParams . get ( 'path' ) ! ;
186
185
const traceViewerServer = clientIdToTraceUrls . get ( event . clientId ?? '' ) ?. traceViewerServer ;
187
186
if ( ! traceViewerServer )
188
187
throw new Error ( 'client is not initialized' ) ;