Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(uses-http2): include multiplexable assets when 1p is a known 3p origin #15638

Merged
merged 3 commits into from
Dec 5, 2023
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
13 changes: 8 additions & 5 deletions core/audits/dobetterweb/uses-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,19 @@
* @param {LH.Artifacts.EntityClassification} classifiedEntities
* @return {boolean}
*/
static isStaticAsset(networkRequest, classifiedEntities) {
static isMultiplexableStaticAsset(networkRequest, classifiedEntities) {
if (!STATIC_RESOURCE_TYPES.has(networkRequest.resourceType)) return false;

// Resources from third-parties that are less than 100 bytes are usually tracking pixels, not actual resources.
// They can masquerade as static types though (gifs, documents, etc)
if (networkRequest.resourceSize < 100) {
// This logic needs to be revisited.
// See https://github.com/GoogleChrome/lighthouse/issues/14661
const entity = classifiedEntities.entityByUrl.get(networkRequest.url);
if (entity && !entity.isUnrecognized) return false;
if (entity) {
// Third-party assets are multiplexable in their first-party context.
if (classifiedEntities.firstParty?.name === entity.name) return true;
// Skip recognizable third-parties' requests.
if (!entity.isUnrecognized) return false;

Check warning on line 166 in core/audits/dobetterweb/uses-http2.js

View check run for this annotation

Codecov / codecov/patch

core/audits/dobetterweb/uses-http2.js#L165-L166

Added lines #L165 - L166 were not covered by tests
}
}

return true;
Expand Down Expand Up @@ -199,7 +202,7 @@
/** @type {Map<string, Array<LH.Artifacts.NetworkRequest>>} */
const groupedByOrigin = new Map();
for (const record of networkRecords) {
if (!UsesHTTP2Audit.isStaticAsset(record, classifiedEntities)) continue;
if (!UsesHTTP2Audit.isMultiplexableStaticAsset(record, classifiedEntities)) continue;
if (UrlUtils.isLikeLocalhost(record.parsedURL.host)) continue;
const existing = groupedByOrigin.get(record.parsedURL.securityOrigin) || [];
existing.push(record);
Expand Down
54 changes: 54 additions & 0 deletions core/test/audits/dobetterweb/uses-http2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,58 @@ describe('Resources are fetched over http/2', () => {
expect(result.score).toEqual(0);
expect(result.metricSavings).toBeUndefined();
});

it('should identify multiplexable assets when run on recognizable 3p origins', async () => {
const networkRecords = [
{
url: 'https://www.twitter.com/',
priority: 'High',
protocol: 'HTTP/1.1',
},
{
url: 'https://www.twitter.com/2',
priority: 'High',
protocol: 'HTTP/1.1',
},
{
url: 'https://www.twitter.com/3',
priority: 'High',
protocol: 'HTTP/1.1',
},
{
url: 'https://www.twitter.com/4',
priority: 'High',
protocol: 'HTTP/1.1',
},
{
url: 'https://www.twitter.com/5',
priority: 'High',
protocol: 'HTTP/1.1',
},
{
url: 'https://www.twitter.com/embed/foo',
priority: 'High',
protocol: 'HTTP/1.1',
},
{
url: 'https://www.facebook.com/embed',
protocol: 'HTTP/1.1',
priority: 'High',
},
];
const artifacts = buildArtifacts(networkRecords);
artifacts.devtoolsLogs.defaultPass = networkRecordsToDevtoolsLog(networkRecords);

const result = await UsesHTTP2Audit.audit(artifacts, context);
const urls = new Set(result.details.items.map(item => item.url));
const hosts = new Set(result.details.items.map(item => new URL(item.url).host));

// Make sure we don't pull in actual 3p domains.
expect(hosts).toEqual(new Set(['www.twitter.com']));

// Make sure we dont flag the 3rd party request for multiplexing.
expect(urls).not.toContain('https://www.facebook.com/embed');

expect(result.details.items).toHaveLength(6);
});
});
Loading