coach-core
Version:
Core package for the Coach.
69 lines (62 loc) • 2.1 kB
JavaScript
;
let util = require('../util');
module.exports = {
id: 'fewRequestsPerDomain',
title: 'Avoid too many requests per domain [HTTP/1]',
description:
'Browsers have a limit on how many concurrent requests they can do per domain when using HTTP/1. When you hit the limit, the browser will wait before it can download more assets on that domain. So avoid having too many requests per domain.',
weight: 5,
tags: ['performance', 'HTTP/1'],
processPage: function (page) {
let limit = 30;
if (util.isHTTP2(page)) {
return {
score: 100,
offending: [],
advice:
'There are almost no limits on HTTP/2 connections with the number of requests, but that is not completely true. It depends on how they are downloaded. Please check your HAR file, does it look OK?'
};
} else if (util.isHTTP3(page)) {
return {
score: 100,
offending: [],
advice:
'HTTP/3 connections is new and there are no best practices at the moment. Please check your HAR file, does it look OK?'
};
}
const infoPerDomain = page.domains;
let offending = [];
let domainAndRequests = {};
let score = 100;
let advice = '';
Object.keys(infoPerDomain).reduce((result, domain) => {
if (infoPerDomain[domain].requests > limit) {
score -= 10;
offending.push(domain);
domainAndRequests[domain] = infoPerDomain[domain].requests;
}
return domainAndRequests;
});
if (score < 100) {
advice =
'The page has ' +
util.plural(offending.length, 'domain') +
' that serves more than ' +
limit +
' requests. ';
Object.keys(domainAndRequests).forEach(function (domain) {
advice +=
domain +
' got ' +
util.plural(domainAndRequests[domain], 'request') +
'. ';
});
advice += 'Improve performance by sharding those or move to HTTP/2.';
}
return {
score: Math.max(0, score),
offending: offending,
advice: advice
};
}
};