fix(body): fetch body explicitly for prefetched scripts (#14941)

This commit is contained in:
Pavel Feldman 2022-06-16 21:07:43 -08:00 коммит произвёл GitHub
Родитель be64e9ce66
Коммит e9069bef6b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 31 добавлений и 1 удалений

Просмотреть файл

@ -271,8 +271,25 @@ export class CRNetworkManager {
_createResponse(request: InterceptableRequest, responsePayload: Protocol.Network.Response): network.Response {
const getResponseBody = async () => {
const contentLengthHeader = Object.entries(responsePayload.headers).find(header => header[0].toLowerCase() === 'content-length');
const expectedLength = contentLengthHeader ? +contentLengthHeader[1] : undefined;
const response = await this._client.send('Network.getResponseBody', { requestId: request._requestId });
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
if (response.body || !expectedLength)
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
// For <link prefetch we are going to receive empty body with non-emtpy content-length expectation. Reach out for the actual content.
const resource = await this._client.send('Network.loadNetworkResource', { url: request.request.url(), frameId: request.request.frame()._id, options: { disableCache: false, includeCredentials: true } });
const chunks: Buffer[] = [];
while (resource.resource.stream) {
const chunk = await this._client.send('IO.read', { handle: resource.resource.stream });
chunks.push(Buffer.from(chunk.data, chunk.base64Encoded ? 'base64' : 'utf-8'));
if (chunk.eof) {
await this._client.send('IO.close', { handle: resource.resource.stream });
break;
}
}
return Buffer.concat(chunks);
};
const timingPayload = responsePayload.timing!;
let timing: network.ResourceTiming;

Просмотреть файл

@ -0,0 +1 @@
<link rel="prefetch" href="prefetch.js">

1
tests/assets/prefetch.js Normal file
Просмотреть файл

@ -0,0 +1 @@
// Scripts will be pre-fetched

Просмотреть файл

@ -334,3 +334,13 @@ it('should report if request was fromServiceWorker', async ({ page, server, isAn
expect(res.fromServiceWorker()).toBe(true);
}
});
it('should return body for prefetch script', async ({ page, server, browserName }) => {
it.skip(browserName === 'webkit', 'No prefetch in WebKit: https://caniuse.com/link-rel-prefetch');
const [response] = await Promise.all([
page.waitForResponse('**/prefetch.js'),
page.goto(server.PREFIX + '/prefetch.html')
]);
const body = await response.body();
expect(body.toString()).toBe('// Scripts will be pre-fetched');
});

Просмотреть файл

@ -64,6 +64,7 @@ it('should fulfill response with empty body', async ({ page, server, isAndroid,
const response = await page.request.fetch(route.request());
await route.fulfill({
response,
headers: { 'content-length': '0' },
status: 201,
body: ''
});