Skip to content

Commit adaef27

Browse files
authored
Revert "fix(trace): should survive ping as the first command after restart" (#37689)
1 parent d2a6174 commit adaef27

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

packages/trace-viewer/src/sw/main.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,16 @@ async function doFetch(event: FetchEvent): Promise<Response> {
9393
const request = event.request;
9494
const client = await self.clients.get(event.clientId);
9595

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-
10996
// When trace viewer is deployed over https, we will force upgrade
11097
// insecure http subresources to https. Otherwise, these will fail
11198
// to load inside our https snapshots.
11299
// In this case, we also match http resources from the archive by
113100
// the https urls.
114101
const isDeployedAsHttps = self.registration.scope.startsWith('https://');
115102

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);
117106
if (relativePath === '/ping') {
118107
await gc();
119108
return new Response(null, { status: 200 });
@@ -123,12 +112,12 @@ async function doFetch(event: FetchEvent): Promise<Response> {
123112
return new Response(null, { status: 200 });
124113
}
125114

126-
const traceUrl = urlInScope.searchParams.get('trace');
115+
const traceUrl = url.searchParams.get('trace');
127116

128117
if (relativePath === '/contexts') {
129118
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) => {
132121
client.postMessage({ method: 'progress', params: { done, total } });
133122
});
134123
return new Response(JSON.stringify(traceModel!.contextEntries), {
@@ -143,20 +132,30 @@ async function doFetch(event: FetchEvent): Promise<Response> {
143132
}
144133
}
145134

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+
146145
if (relativePath.startsWith('/snapshotInfo/')) {
147146
const { snapshotServer } = loadedTraces.get(traceUrl!) || {};
148147
if (!snapshotServer)
149148
return new Response(null, { status: 404 });
150149
const pageOrFrameId = relativePath.substring('/snapshotInfo/'.length);
151-
return snapshotServer.serveSnapshotInfo(pageOrFrameId, urlInScope.searchParams);
150+
return snapshotServer.serveSnapshotInfo(pageOrFrameId, url.searchParams);
152151
}
153152

154153
if (relativePath.startsWith('/snapshot/')) {
155154
const { snapshotServer } = loadedTraces.get(traceUrl!) || {};
156155
if (!snapshotServer)
157156
return new Response(null, { status: 404 });
158157
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);
160159
if (isDeployedAsHttps)
161160
response.headers.set('Content-Security-Policy', 'upgrade-insecure-requests');
162161
return response;
@@ -167,7 +166,7 @@ async function doFetch(event: FetchEvent): Promise<Response> {
167166
if (!snapshotServer)
168167
return new Response(null, { status: 404 });
169168
const pageOrFrameId = relativePath.substring('/closest-screenshot/'.length);
170-
return snapshotServer.serveClosestScreenshot(pageOrFrameId, urlInScope.searchParams);
169+
return snapshotServer.serveClosestScreenshot(pageOrFrameId, url.searchParams);
171170
}
172171

173172
if (relativePath.startsWith('/sha1/')) {
@@ -176,13 +175,13 @@ async function doFetch(event: FetchEvent): Promise<Response> {
176175
for (const trace of loadedTraces.values()) {
177176
const blob = await trace.traceModel.resourceForSha1(sha1);
178177
if (blob)
179-
return new Response(blob, { status: 200, headers: downloadHeaders(urlInScope.searchParams) });
178+
return new Response(blob, { status: 200, headers: downloadHeaders(url.searchParams) });
180179
}
181180
return new Response(null, { status: 404 });
182181
}
183182

184183
if (relativePath.startsWith('/file/')) {
185-
const path = urlInScope.searchParams.get('path')!;
184+
const path = url.searchParams.get('path')!;
186185
const traceViewerServer = clientIdToTraceUrls.get(event.clientId ?? '')?.traceViewerServer;
187186
if (!traceViewerServer)
188187
throw new Error('client is not initialized');

tests/library/trace-viewer.spec.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,23 +2056,3 @@ test('should survive service worker restart', async ({ page, runAndTrace, server
20562056
const snapshot2 = await traceViewer.snapshotFrame('Set content');
20572057
await expect(snapshot2.locator('body')).toHaveText('Old world');
20582058
});
2059-
2060-
test('should survive ping after service worker restart', async ({ page, runAndTrace, server }) => {
2061-
const traceViewer = await runAndTrace(async () => {
2062-
await page.goto(server.EMPTY_PAGE);
2063-
await page.setContent('Old world');
2064-
await page.evaluate(() => document.body.textContent = 'New world');
2065-
});
2066-
const snapshot1 = await traceViewer.snapshotFrame('Evaluate');
2067-
await expect(snapshot1.locator('body')).toHaveText('New world');
2068-
2069-
const status = await traceViewer.page.evaluate(async () => {
2070-
const response1 = await fetch('restartServiceWorker');
2071-
const response2 = await fetch('ping');
2072-
return response1.status + '/' + response2.status;
2073-
});
2074-
expect(status).toBe('200/200');
2075-
2076-
const snapshot2 = await traceViewer.snapshotFrame('Set content');
2077-
await expect(snapshot2.locator('body')).toHaveText('Old world');
2078-
});

0 commit comments

Comments
 (0)