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-rel-preconnect): warn on 3+ preconnects #9903

Merged
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
11 changes: 11 additions & 0 deletions lighthouse-core/audits/uses-rel-preconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const UIStrings = {
* */
crossoriginWarning: 'A preconnect <link> was found for "{securityOrigin}" but was not used ' +
'by the browser. Check that you are using the `crossorigin` attribute properly.',
/** A warning message that is shown when found more than 2 preconnected links */
tooManyPreconnectLinksWarning: 'More than 2 preconnect links were found. ' +
'Preconnect links should be used sparingly and only to the most important origins.',
};

const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
Expand Down Expand Up @@ -135,6 +138,14 @@ class UsesRelPreconnectAudit extends Audit {
const preconnectLinks = artifacts.LinkElements.filter(el => el.rel === 'preconnect');
const preconnectOrigins = new Set(preconnectLinks.map(link => URL.getOrigin(link.href || '')));

// https://twitter.com/_tbansal/status/1197771385172480001
if (preconnectLinks.length >= 3) {
return {
score: 1,
warnings: [str_(UIStrings.tooManyPreconnectLinksWarning)],
};
}

/** @type {Array<{url: string, wastedMs: number}>}*/
let results = [];
origins.forEach(records => {
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-core/lib/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,9 @@
"lighthouse-core/audits/uses-rel-preconnect.js | title": {
"message": "Preconnect to required origins"
},
"lighthouse-core/audits/uses-rel-preconnect.js | tooManyPreconnectLinksWarning": {
"message": "More than 2 preconnect links were found. Preconnect links should be used sparingly and only to the most important origins."
},
"lighthouse-core/audits/uses-rel-preload.js | crossoriginWarning": {
"message": "A preload <link> was found for \"{preloadURL}\" but was not used by the browser. Check that you are using the `crossorigin` attribute properly."
},
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-core/lib/i18n/locales/en-XL.json
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,9 @@
"lighthouse-core/audits/uses-rel-preconnect.js | title": {
"message": "P̂ŕêćôńn̂éĉt́ t̂ó r̂éq̂úîŕêd́ ôŕîǵîńŝ"
},
"lighthouse-core/audits/uses-rel-preconnect.js | tooManyPreconnectLinksWarning": {
"message": "M̂ór̂é t̂h́âń 2 p̂ŕêćôńn̂éĉt́ l̂ín̂ḱŝ ẃêŕê f́ôún̂d́. P̂ŕêćôńn̂éĉt́ l̂ín̂ḱŝ śĥóûĺd̂ b́ê úŝéd̂ śp̂ár̂ín̂ǵl̂ý âńd̂ ón̂ĺŷ t́ô t́ĥé m̂óŝt́ îḿp̂ór̂t́âńt̂ ór̂íĝín̂ś."
},
"lighthouse-core/audits/uses-rel-preload.js | crossoriginWarning": {
"message": "Â ṕr̂él̂óâd́ <l̂ín̂ḱ> ŵáŝ f́ôún̂d́ f̂ór̂ \"{preloadURL}\" b́ût́ ŵáŝ ńôt́ ûśêd́ b̂ý t̂h́ê b́r̂óŵśêŕ. Ĉh́êćk̂ t́ĥát̂ ýôú âŕê úŝín̂ǵ t̂h́ê `crossorigin` át̂t́r̂íb̂út̂é p̂ŕôṕêŕl̂ý."
},
Expand Down
38 changes: 37 additions & 1 deletion lighthouse-core/test/audits/uses-rel-preconnect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,48 @@ describe('Performance: uses-rel-preconnect audit', () => {
};

const context = {settings: {}, computedCache: new Map()};
const {numericValue, extendedInfo} = await UsesRelPreconnect.audit(artifacts, context);
const {
numericValue,
extendedInfo,
warnings,
} = await UsesRelPreconnect.audit(artifacts, context);
assert.equal(numericValue, 300);
assert.equal(extendedInfo.value.length, 2);
assert.deepStrictEqual(extendedInfo.value, [
{url: 'https://othercdn.example.com', wastedMs: 300},
{url: 'http://cdn.example.com', wastedMs: 150},
]);
assert.equal(warnings.length, 0);
});

it('should pass with a warning if too many preconnects found', async () => {
const networkRecords = [
mainResource,
{
url: 'http://cdn.example.com/first',
initiator: {},
startTime: 2,
timing: {
dnsStart: 100,
connectStart: 250,
connectEnd: 300,
receiveHeadersEnd: 2.3,
},
},
];
const artifacts = {
LinkElements: [
{rel: 'preconnect', href: 'https://cdn1.example.com/'},
{rel: 'preconnect', href: 'https://cdn2.example.com/'},
{rel: 'preconnect', href: 'https://cdn3.example.com/'},
],
devtoolsLogs: {[UsesRelPreconnect.DEFAULT_PASS]: networkRecordsToDevtoolsLog(networkRecords)},
URL: {finalUrl: mainResource.url},
};

const context = {settings: {}, computedCache: new Map()};
const result = await UsesRelPreconnect.audit(artifacts, context);
assert.equal(result.score, 1);
assert.equal(result.warnings.length, 1);
});
});