Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/playwright-core/src/server/chromium/crNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export class CRNetworkManager {
// @see https://crbug.com/750469
if (!request)
return;
this._maybeUpdateOOPIFMainRequest(sessionInfo, request);
this._maybeUpdateRequestSession(sessionInfo, request);

// Under certain conditions we never get the Network.responseReceived
// event from protocol. @see https://crbug.com/883475
Expand Down Expand Up @@ -525,7 +525,7 @@ export class CRNetworkManager {
// @see https://crbug.com/750469
if (!request)
return;
this._maybeUpdateOOPIFMainRequest(sessionInfo, request);
this._maybeUpdateRequestSession(sessionInfo, request);
const response = request.request._existingResponse();
if (response) {
response.setTransferSize(null);
Expand All @@ -540,11 +540,17 @@ export class CRNetworkManager {
(this._page?._frameManager || this._serviceWorker)!.requestFailed(request.request, !!event.canceled);
}

private _maybeUpdateOOPIFMainRequest(sessionInfo: SessionInfo, request: InterceptableRequest) {
private _maybeUpdateRequestSession(sessionInfo: SessionInfo, request: InterceptableRequest) {
// OOPIF has a main request that starts in the parent session but finishes in the child session.
// We check for the main request by matching loaderId and requestId, and if it now belongs to
// a child session, migrate it there.
if (request.session !== sessionInfo.session && !sessionInfo.isMain && request._documentId === request._requestId)
//
// Same goes for the main worker script with PlzDedicatedWorker enabled, which is the default.
// Here we check the `workerFrame`.
//
// In theory, we can always update the session. However, we try to be conservative here
// to make sure we understand all the scenarios where the session should be updated.
if (request.session !== sessionInfo.session && !sessionInfo.isMain && (request._documentId === request._requestId || sessionInfo.workerFrame))
request.session = sessionInfo.session;
}
}
Expand Down
33 changes: 32 additions & 1 deletion tests/page/workers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ it('should report network activity on worker creation', async function({ page, s
});

it('should report worker script as network request', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33107' },
annotation: [
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33107' },
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35678' },
],
}, async function({ page, server }) {
await page.goto(server.EMPTY_PAGE);
const [request1, request2] = await Promise.all([
Expand All @@ -192,6 +195,34 @@ it('should report worker script as network request', {
]);
expect.soft(request1.url()).toBe(server.PREFIX + '/worker/worker.js');
expect.soft(request1).toBe(request2);
const response = await request1.response();
const text = await response.text();
expect(text).toContain(`console.log('hello from the worker');`);
});

it('should report worker script as network request after redirect', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35678' },
}, async ({ page, server, browserName }) => {
it.fixme(browserName === 'chromium', 'Chromium does not report the redirect because it is not plumbed to the worker target');

await page.goto(server.EMPTY_PAGE);
server.setRedirect('/worker.js', '/worker2.js');
server.setRoute('/worker2.js', (req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.end(`console.log('hello from the worker');`);
});
const [request] = await Promise.all([
page.waitForEvent('request', r => r.url().includes('worker.js')),
page.waitForEvent('console', msg => msg.text().includes('hello from the worker')),
page.evaluate(() => (window as any).w = new Worker('/worker.js')),
]);
expect(request.url()).toBe(server.PREFIX + '/worker.js');
const redirect = request.redirectedTo();
expect(redirect).toBeTruthy();
expect(redirect.url()).toBe(server.PREFIX + '/worker2.js');
const response = await redirect.response();
const text = await response.text();
expect(text).toContain(`console.log('hello from the worker');`);
});

it('should dispatch console messages when page has workers', async function({ page, server }) {
Expand Down