Repository: GoogleChrome/lighthouse
Pull request #10293 (2020-02-04 15:49:01)

core(preconnect): pass without warning at 2 links
**Summary**
https://github.com/GoogleChrome/lighthouse/pull/9903 added a warning when you went over 2 preconnect links but we didn't stop suggesting them at 2 leading to an odd UX of us telling you to add more but then warning when you do.

**Related Issues/PRs**
https://github.com/GoogleChrome/lighthouse/pull/9903
https://github.com/GoogleChrome/lighthouse/issues/10292



Patch:
diff --git a/lighthouse-core/audits/uses-rel-preconnect.js b/lighthouse-core/audits/uses-rel-preconnect.js
index 7f260dd47..c74e3f694 100644
--- a/lighthouse-core/audits/uses-rel-preconnect.js
+++ b/lighthouse-core/audits/uses-rel-preconnect.js
@@ -138,14 +138,6 @@ 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 => {
@@ -192,6 +184,16 @@ class UsesRelPreconnectAudit extends Audit {
     results = results
       .sort((a, b) => b.wastedMs - a.wastedMs);
 
+    // Shortcut early with a pass when the user has already configured preconnect.
+    // https://twitter.com/_tbansal/status/1197771385172480001
+    if (preconnectLinks.length >= 2) {
+      return {
+        score: 1,
+        warnings: preconnectLinks.length >= 3 ?
+          [...warnings, str_(UIStrings.tooManyPreconnectLinksWarning)] : warnings,
+      };
+    }
+
     /** @type {LH.Audit.Details.Opportunity['headings']} */
     const headings = [
       {key: 'url', valueType: 'url', label: str_(i18n.UIStrings.columnURL)},
diff --git a/lighthouse-core/lib/stack-collector.js b/lighthouse-core/lib/stack-collector.js
index 8fd8633fe..61a8aed2f 100644
--- a/lighthouse-core/lib/stack-collector.js
+++ b/lighthouse-core/lib/stack-collector.js
@@ -17,7 +17,7 @@ const fs = require('fs');
 const libDetectorSource = fs.readFileSync(
   require.resolve('js-library-detector/library/libraries.js'), 'utf8');
 
-/** @typedef {false | {version: string|null}} JSLibraryDetectorTestResult */
+/** @typedef {false | {version: string|number|null}} JSLibraryDetectorTestResult */
 /**
  * @typedef JSLibraryDetectorTest
  * @property {string} id
@@ -31,7 +31,7 @@ const libDetectorSource = fs.readFileSync(
  * @typedef JSLibrary
  * @property {string} id
  * @property {string} name
- * @property {string|null} version
+ * @property {string|number|null} version
  * @property {string|null} npm
  */
 
@@ -47,7 +47,7 @@ async function detectLibraries() {
   // see https://github.com/HTTPArchive/httparchive/issues/77#issuecomment-291320900
   /** @type {Record<string, JSLibraryDetectorTest>} */
   // @ts-ignore - injected libDetectorSource var
-  const libraryDetectorTests = d41d8cd98f00b204e9800998ecf8427e_LibraryDetectorTests; // eslint-disable-line 
+  const libraryDetectorTests = d41d8cd98f00b204e9800998ecf8427e_LibraryDetectorTests; // eslint-disable-line
 
   for (const [name, lib] of Object.entries(libraryDetectorTests)) {
     try {
@@ -83,7 +83,7 @@ async function collectStacks(passContext) {
     detector: /** @type {'js'} */ ('js'),
     id: lib.id,
     name: lib.name,
-    version: lib.version || undefined,
+    version: typeof lib.version === 'number' ? String(lib.version) : (lib.version || undefined),
     npm: lib.npm || undefined,
   }));
 }
diff --git a/lighthouse-core/scripts/git3po-rules/add-needs-priority.yaml b/lighthouse-core/scripts/git3po-rules/add-needs-priority.yaml
new file mode 100644
index 000000000..8e949251d
--- /dev/null
+++ b/lighthouse-core/scripts/git3po-rules/add-needs-priority.yaml
@@ -0,0 +1,17 @@
+repo: GoogleChrome/lighthouse
+filters:
+  - type: issue
+    criteria:
+      state: open
+      labels:
+        $not:
+          $or:
+            - $includes: needs-priority
+            - $includes: P0
+            - $includes: P1
+            - $includes: P1.5
+            - $includes: P2
+            - $includes: P3
+actions:
+  - type: add_label
+    label: needs-priority
diff --git a/lighthouse-core/scripts/git3po-rules/remove-needs-priority.yaml b/lighthouse-core/scripts/git3po-rules/remove-needs-priority.yaml
new file mode 100644
index 000000000..f5160c06a
--- /dev/null
+++ b/lighthouse-core/scripts/git3po-rules/remove-needs-priority.yaml
@@ -0,0 +1,17 @@
+repo: GoogleChrome/lighthouse
+filters:
+  - type: issue
+    criteria:
+      state: open
+      labels:
+        $and:
+          - $includes: needs-priority
+          - $or:
+            - $includes: P0
+            - $includes: P1
+            - $includes: P1.5
+            - $includes: P2
+            - $includes: P3
+actions:
+  - type: remove_label
+    label: needs-priority


Test patch:
diff --git a/lighthouse-cli/test/smokehouse/test-definitions/perf/expectations.js b/lighthouse-cli/test/smokehouse/test-definitions/perf/expectations.js
index ffdd83e95..3cfcb392b 100644
--- a/lighthouse-cli/test/smokehouse/test-definitions/perf/expectations.js
+++ b/lighthouse-cli/test/smokehouse/test-definitions/perf/expectations.js
@@ -52,16 +52,11 @@ module.exports = [
           },
         },
         'uses-rel-preconnect': {
-          score: '<1',
+          score: 1,
           warnings: {
             0: /fonts.googleapis/,
             length: 1,
           },
-          details: {
-            items: {
-              length: 1,
-            },
-          },
         },
       },
     },
diff --git a/lighthouse-core/test/audits/uses-rel-preconnect-test.js b/lighthouse-core/test/audits/uses-rel-preconnect-test.js
index 073e805b4..0decd6e5e 100644
--- a/lighthouse-core/test/audits/uses-rel-preconnect-test.js
+++ b/lighthouse-core/test/audits/uses-rel-preconnect-test.js
@@ -256,6 +256,36 @@ describe('Performance: uses-rel-preconnect audit', () => {
     assert.equal(warnings.length, 0);
   });
 
+  it('should pass if the correct number of 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/'},
+      ],
+      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.deepStrictEqual(result.warnings, []);
+  });
+
   it('should pass with a warning if too many preconnects found', async () => {
     const networkRecords = [
       mainResource,
diff --git a/lighthouse-core/test/lib/stack-collector-test.js b/lighthouse-core/test/lib/stack-collector-test.js
new file mode 100644
index 000000000..758593788
--- /dev/null
+++ b/lighthouse-core/test/lib/stack-collector-test.js
@@ -0,0 +1,33 @@
+/**
+ * @license Copyright 2020 Google Inc. All Rights Reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
+ */
+'use strict';
+
+/* eslint-env jest */
+
+const collectStacks = require('../../lib/stack-collector.js');
+
+describe('stack collector', () => {
+  /** @type {{driver: {evaluateAsync: jest.Mock}}} */
+  let passContext;
+
+  beforeEach(() => {
+    passContext = {driver: {evaluateAsync: jest.fn()}};
+  });
+
+  it('returns the detected stacks', async () => {
+    passContext.driver.evaluateAsync.mockResolvedValue([
+      {id: 'jquery', name: 'jQuery', version: '2.1.0', npm: 'jquery'},
+      {id: 'angular', name: 'Angular', version: '', npm: ''},
+      {id: 'magento', name: 'Magento', version: 2},
+    ]);
+
+    expect(await collectStacks(passContext)).toEqual([
+      {detector: 'js', id: 'jquery', name: 'jQuery', npm: 'jquery', version: '2.1.0'},
+      {detector: 'js', id: 'angular', name: 'Angular', npm: undefined, version: undefined},
+      {detector: 'js', id: 'magento', name: 'Magento', npm: undefined, version: '2'},
+    ]);
+  });
+});