lighthouse 11.7.0-dev.20240327 → 11.7.0-dev.20240328

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/core/computed/metrics/lantern-first-contentful-paint.d.ts +13 -82
  2. package/core/computed/metrics/lantern-first-contentful-paint.js +15 -178
  3. package/core/computed/metrics/lantern-first-meaningful-paint.d.ts +13 -12
  4. package/core/computed/metrics/lantern-first-meaningful-paint.js +10 -49
  5. package/core/computed/metrics/lantern-interactive.d.ts +13 -21
  6. package/core/computed/metrics/lantern-interactive.js +11 -84
  7. package/core/computed/metrics/lantern-largest-contentful-paint.d.ts +13 -25
  8. package/core/computed/metrics/lantern-largest-contentful-paint.js +12 -80
  9. package/core/computed/metrics/lantern-max-potential-fid.d.ts +13 -24
  10. package/core/computed/metrics/lantern-max-potential-fid.js +11 -64
  11. package/core/computed/metrics/lantern-metric.d.ts +9 -18
  12. package/core/computed/metrics/lantern-metric.js +13 -29
  13. package/core/computed/metrics/lantern-speed-index.d.ts +13 -32
  14. package/core/computed/metrics/lantern-speed-index.js +12 -116
  15. package/core/computed/metrics/lantern-total-blocking-time.d.ts +13 -25
  16. package/core/computed/metrics/lantern-total-blocking-time.js +10 -94
  17. package/core/config/default-config.js +40 -49
  18. package/core/config/experimental-config.js +1 -1
  19. package/core/lib/lantern/metrics/first-contentful-paint.d.ts +79 -0
  20. package/core/lib/lantern/metrics/first-contentful-paint.js +200 -0
  21. package/core/lib/lantern/metrics/first-meaningful-paint.d.ts +6 -0
  22. package/core/lib/lantern/metrics/first-meaningful-paint.js +66 -0
  23. package/core/lib/lantern/metrics/interactive.d.ts +26 -0
  24. package/core/lib/lantern/metrics/interactive.js +112 -0
  25. package/core/lib/lantern/metrics/largest-contentful-paint.d.ts +19 -0
  26. package/core/lib/lantern/metrics/largest-contentful-paint.js +108 -0
  27. package/core/lib/lantern/metrics/max-potential-fid.d.ts +30 -0
  28. package/core/lib/lantern/metrics/max-potential-fid.js +92 -0
  29. package/core/lib/lantern/metrics/speed-index.d.ts +38 -0
  30. package/core/lib/lantern/metrics/speed-index.js +145 -0
  31. package/core/lib/lantern/metrics/total-blocking-time.d.ts +31 -0
  32. package/core/lib/lantern/metrics/total-blocking-time.js +128 -0
  33. package/core/lib/lighthouse-compatibility.js +21 -11
  34. package/dist/report/bundle.esm.js +1 -1
  35. package/dist/report/flow.js +2 -2
  36. package/dist/report/standalone.js +1 -1
  37. package/package.json +1 -1
  38. package/report/renderer/performance-category-renderer.d.ts +0 -7
  39. package/report/renderer/performance-category-renderer.js +1 -11
  40. package/shared/localization/locales/ar-XB.json +0 -6
  41. package/shared/localization/locales/ar.json +0 -6
  42. package/shared/localization/locales/bg.json +0 -6
  43. package/shared/localization/locales/ca.json +0 -6
  44. package/shared/localization/locales/cs.json +0 -6
  45. package/shared/localization/locales/da.json +0 -6
  46. package/shared/localization/locales/de.json +0 -6
  47. package/shared/localization/locales/el.json +0 -6
  48. package/shared/localization/locales/en-GB.json +0 -6
  49. package/shared/localization/locales/en-US.json +0 -6
  50. package/shared/localization/locales/en-XA.json +0 -6
  51. package/shared/localization/locales/en-XL.json +0 -6
  52. package/shared/localization/locales/es-419.json +0 -6
  53. package/shared/localization/locales/es.json +0 -6
  54. package/shared/localization/locales/fi.json +0 -6
  55. package/shared/localization/locales/fil.json +0 -6
  56. package/shared/localization/locales/fr.json +0 -6
  57. package/shared/localization/locales/he.json +0 -6
  58. package/shared/localization/locales/hi.json +0 -6
  59. package/shared/localization/locales/hr.json +0 -6
  60. package/shared/localization/locales/hu.json +0 -6
  61. package/shared/localization/locales/id.json +0 -6
  62. package/shared/localization/locales/it.json +0 -6
  63. package/shared/localization/locales/ja.json +0 -6
  64. package/shared/localization/locales/ko.json +0 -6
  65. package/shared/localization/locales/lt.json +0 -6
  66. package/shared/localization/locales/lv.json +0 -6
  67. package/shared/localization/locales/nl.json +0 -6
  68. package/shared/localization/locales/no.json +0 -6
  69. package/shared/localization/locales/pl.json +0 -6
  70. package/shared/localization/locales/pt-PT.json +0 -6
  71. package/shared/localization/locales/pt.json +0 -6
  72. package/shared/localization/locales/ro.json +0 -6
  73. package/shared/localization/locales/ru.json +0 -6
  74. package/shared/localization/locales/sk.json +0 -6
  75. package/shared/localization/locales/sl.json +0 -6
  76. package/shared/localization/locales/sr-Latn.json +0 -6
  77. package/shared/localization/locales/sr.json +0 -6
  78. package/shared/localization/locales/sv.json +0 -6
  79. package/shared/localization/locales/ta.json +0 -6
  80. package/shared/localization/locales/te.json +0 -6
  81. package/shared/localization/locales/th.json +0 -6
  82. package/shared/localization/locales/tr.json +0 -6
  83. package/shared/localization/locales/uk.json +0 -6
  84. package/shared/localization/locales/vi.json +0 -6
  85. package/shared/localization/locales/zh-HK.json +0 -6
  86. package/shared/localization/locales/zh-TW.json +0 -6
  87. package/shared/localization/locales/zh.json +0 -6
  88. package/tsconfig.json +1 -0
@@ -4,83 +4,23 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
 
7
+ import {TotalBlockingTime} from '../../lib/lantern/metrics/total-blocking-time.js';
7
8
  import {makeComputedArtifact} from '../computed-artifact.js';
8
- import {LanternMetric} from './lantern-metric.js';
9
- import {BaseNode} from '../../lib/lantern/base-node.js';
10
9
  import {LanternFirstContentfulPaint} from './lantern-first-contentful-paint.js';
11
10
  import {LanternInteractive} from './lantern-interactive.js';
12
- import {BLOCKING_TIME_THRESHOLD, calculateSumOfBlockingTime} from './tbt-utils.js';
11
+ import {getComputationDataParams} from './lantern-metric.js';
13
12
 
14
- /** @typedef {import('../../lib/lantern/base-node.js').Node<LH.Artifacts.NetworkRequest>} Node */
13
+ /** @typedef {import('../../lib/lantern/metric.js').Extras} Extras */
15
14
 
16
- class LanternTotalBlockingTime extends LanternMetric {
15
+ class LanternTotalBlockingTime extends TotalBlockingTime {
17
16
  /**
18
- * @return {LH.Gatherer.Simulation.MetricCoefficients}
19
- */
20
- static get COEFFICIENTS() {
21
- return {
22
- intercept: 0,
23
- optimistic: 0.5,
24
- pessimistic: 0.5,
25
- };
26
- }
27
-
28
- /**
29
- * @param {Node} dependencyGraph
30
- * @return {Node}
31
- */
32
- static getOptimisticGraph(dependencyGraph) {
33
- return dependencyGraph;
34
- }
35
-
36
- /**
37
- * @param {Node} dependencyGraph
38
- * @return {Node}
39
- */
40
- static getPessimisticGraph(dependencyGraph) {
41
- return dependencyGraph;
42
- }
43
-
44
- /**
45
- * @param {LH.Gatherer.Simulation.Result} simulation
46
- * @param {import('../../lib/lantern/metric.js').Extras} extras
47
- * @return {LH.Gatherer.Simulation.Result}
17
+ * @param {LH.Artifacts.MetricComputationDataInput} data
18
+ * @param {LH.Artifacts.ComputedContext} context
19
+ * @param {Omit<Extras, 'optimistic'>=} extras
20
+ * @return {Promise<LH.Artifacts.LanternMetric>}
48
21
  */
49
- static getEstimateFromSimulation(simulation, extras) {
50
- if (!extras.fcpResult) throw new Error('missing fcpResult');
51
- if (!extras.interactiveResult) throw new Error('missing interactiveResult');
52
-
53
- // Intentionally use the opposite FCP estimate. A pessimistic FCP is higher than equal to an
54
- // optimistic FCP, which means potentially more tasks are excluded from the Total Blocking Time
55
- // computation. So a more pessimistic FCP gives a more optimistic Total Blocking Time for the
56
- // same work.
57
- const fcpTimeInMs = extras.optimistic
58
- ? extras.fcpResult.pessimisticEstimate.timeInMs
59
- : extras.fcpResult.optimisticEstimate.timeInMs;
60
-
61
- // Similarly, we always have pessimistic TTI >= optimistic TTI. Therefore, picking optimistic
62
- // TTI means our window of interest is smaller and thus potentially more tasks are excluded from
63
- // Total Blocking Time computation, yielding a lower (more optimistic) Total Blocking Time value
64
- // for the same work.
65
- const interactiveTimeMs = extras.optimistic
66
- ? extras.interactiveResult.optimisticEstimate.timeInMs
67
- : extras.interactiveResult.pessimisticEstimate.timeInMs;
68
-
69
- const minDurationMs = BLOCKING_TIME_THRESHOLD;
70
-
71
- const events = LanternTotalBlockingTime.getTopLevelEvents(
72
- simulation.nodeTimings,
73
- minDurationMs
74
- );
75
-
76
- return {
77
- timeInMs: calculateSumOfBlockingTime(
78
- events,
79
- fcpTimeInMs,
80
- interactiveTimeMs
81
- ),
82
- nodeTimings: simulation.nodeTimings,
83
- };
22
+ static async computeMetricWithGraphs(data, context, extras) {
23
+ return this.compute(await getComputationDataParams(data, context), extras);
84
24
  }
85
25
 
86
26
  /**
@@ -93,30 +33,6 @@ class LanternTotalBlockingTime extends LanternMetric {
93
33
  const interactiveResult = await LanternInteractive.request(data, context);
94
34
  return this.computeMetricWithGraphs(data, context, {fcpResult, interactiveResult});
95
35
  }
96
-
97
- /**
98
- * @param {LH.Gatherer.Simulation.Result['nodeTimings']} nodeTimings
99
- * @param {number} minDurationMs
100
- */
101
- static getTopLevelEvents(nodeTimings, minDurationMs) {
102
- /** @type {Array<{start: number, end: number, duration: number}>}
103
- */
104
- const events = [];
105
-
106
- for (const [node, timing] of nodeTimings.entries()) {
107
- if (node.type !== BaseNode.TYPES.CPU) continue;
108
- // Filtering out events below minimum duration.
109
- if (timing.duration < minDurationMs) continue;
110
-
111
- events.push({
112
- start: timing.startTime,
113
- end: timing.endTime,
114
- duration: timing.duration,
115
- });
116
- }
117
-
118
- return events;
119
- }
120
36
  }
121
37
 
122
38
  const LanternTotalBlockingTimeComputed = makeComputedArtifact(
@@ -19,10 +19,6 @@ const UIStrings = {
19
19
  budgetsGroupDescription: 'Performance budgets set standards for the performance of your site.',
20
20
  /** Title of the speed metrics section of the Performance category. Within this section are various speed metrics which quantify the pageload performance into values presented in seconds and milliseconds. */
21
21
  metricGroupTitle: 'Metrics',
22
- /** Title of the opportunity section of the Performance category. Within this section are audits with imperative titles that suggest actions the user can take to improve the loading performance of their web page. 'Suggestion'/'Optimization'/'Recommendation' are reasonable synonyms for 'opportunity' in this case. */
23
- loadOpportunitiesGroupTitle: 'Opportunities',
24
- /** Description of the opportunity section of the Performance category. 'Suggestions' could also be 'recommendations'. Within this section are audits with imperative titles that suggest actions the user can take to improve the loading performance of their web page. */
25
- loadOpportunitiesGroupDescription: 'These suggestions can help your page load faster. They don\'t [directly affect](https://developer.chrome.com/docs/lighthouse/performance/performance-scoring/) the Performance score.',
26
22
  /** Title of an opportunity sub-section of the Performance category. Within this section are audits with imperative titles that suggest actions the user can take to improve the time of the first initial render of the webpage. */
27
23
  firstPaintImprovementsGroupTitle: 'First Paint Improvements',
28
24
  /** Description of an opportunity sub-section of the Performance category. Within this section are audits with imperative titles that suggest actions the user can take to improve the time of the first initial render of the webpage. */
@@ -346,10 +342,6 @@ const defaultConfig = {
346
342
  'metrics': {
347
343
  title: str_(UIStrings.metricGroupTitle),
348
344
  },
349
- 'load-opportunities': {
350
- title: str_(UIStrings.loadOpportunitiesGroupTitle),
351
- description: str_(UIStrings.loadOpportunitiesGroupDescription),
352
- },
353
345
  'budgets': {
354
346
  title: str_(UIStrings.budgetsGroupTitle),
355
347
  description: str_(UIStrings.budgetsGroupDescription),
@@ -440,47 +432,46 @@ const defaultConfig = {
440
432
  {id: 'max-potential-fid', weight: 0, group: 'hidden'},
441
433
  {id: 'first-meaningful-paint', weight: 0, acronym: 'FMP', group: 'hidden'},
442
434
 
443
- // These audits will be put in "load-opportunities" or "diagnostics" based on their details type.
444
- {id: 'render-blocking-resources', weight: 0},
445
- {id: 'uses-responsive-images', weight: 0},
446
- {id: 'offscreen-images', weight: 0},
447
- {id: 'unminified-css', weight: 0},
448
- {id: 'unminified-javascript', weight: 0},
449
- {id: 'unused-css-rules', weight: 0},
450
- {id: 'unused-javascript', weight: 0},
451
- {id: 'uses-optimized-images', weight: 0},
452
- {id: 'modern-image-formats', weight: 0},
453
- {id: 'uses-text-compression', weight: 0},
454
- {id: 'uses-rel-preconnect', weight: 0},
455
- {id: 'server-response-time', weight: 0},
456
- {id: 'redirects', weight: 0},
457
- {id: 'uses-http2', weight: 0},
458
- {id: 'efficient-animated-content', weight: 0},
459
- {id: 'duplicated-javascript', weight: 0},
460
- {id: 'legacy-javascript', weight: 0},
461
- {id: 'prioritize-lcp-image', weight: 0},
462
- {id: 'total-byte-weight', weight: 0},
463
- {id: 'uses-long-cache-ttl', weight: 0},
464
- {id: 'dom-size', weight: 0},
465
- {id: 'critical-request-chains', weight: 0},
466
- {id: 'user-timings', weight: 0},
467
- {id: 'bootup-time', weight: 0},
468
- {id: 'mainthread-work-breakdown', weight: 0},
469
- {id: 'font-display', weight: 0},
470
- {id: 'third-party-summary', weight: 0},
471
- {id: 'third-party-facades', weight: 0},
472
- {id: 'largest-contentful-paint-element', weight: 0},
473
- {id: 'lcp-lazy-loaded', weight: 0},
474
- {id: 'layout-shifts', weight: 0},
475
- {id: 'uses-passive-event-listeners', weight: 0},
476
- {id: 'no-document-write', weight: 0},
477
- {id: 'long-tasks', weight: 0},
478
- {id: 'non-composited-animations', weight: 0},
479
- {id: 'unsized-images', weight: 0},
480
- {id: 'viewport', weight: 0},
481
- {id: 'uses-responsive-images-snapshot', weight: 0},
482
- {id: 'work-during-interaction', weight: 0},
483
- {id: 'bf-cache', weight: 0},
435
+ {id: 'render-blocking-resources', weight: 0, group: 'diagnostics'},
436
+ {id: 'uses-responsive-images', weight: 0, group: 'diagnostics'},
437
+ {id: 'offscreen-images', weight: 0, group: 'diagnostics'},
438
+ {id: 'unminified-css', weight: 0, group: 'diagnostics'},
439
+ {id: 'unminified-javascript', weight: 0, group: 'diagnostics'},
440
+ {id: 'unused-css-rules', weight: 0, group: 'diagnostics'},
441
+ {id: 'unused-javascript', weight: 0, group: 'diagnostics'},
442
+ {id: 'uses-optimized-images', weight: 0, group: 'diagnostics'},
443
+ {id: 'modern-image-formats', weight: 0, group: 'diagnostics'},
444
+ {id: 'uses-text-compression', weight: 0, group: 'diagnostics'},
445
+ {id: 'uses-rel-preconnect', weight: 0, group: 'diagnostics'},
446
+ {id: 'server-response-time', weight: 0, group: 'diagnostics'},
447
+ {id: 'redirects', weight: 0, group: 'diagnostics'},
448
+ {id: 'uses-http2', weight: 0, group: 'diagnostics'},
449
+ {id: 'efficient-animated-content', weight: 0, group: 'diagnostics'},
450
+ {id: 'duplicated-javascript', weight: 0, group: 'diagnostics'},
451
+ {id: 'legacy-javascript', weight: 0, group: 'diagnostics'},
452
+ {id: 'prioritize-lcp-image', weight: 0, group: 'diagnostics'},
453
+ {id: 'total-byte-weight', weight: 0, group: 'diagnostics'},
454
+ {id: 'uses-long-cache-ttl', weight: 0, group: 'diagnostics'},
455
+ {id: 'dom-size', weight: 0, group: 'diagnostics'},
456
+ {id: 'critical-request-chains', weight: 0, group: 'diagnostics'},
457
+ {id: 'user-timings', weight: 0, group: 'diagnostics'},
458
+ {id: 'bootup-time', weight: 0, group: 'diagnostics'},
459
+ {id: 'mainthread-work-breakdown', weight: 0, group: 'diagnostics'},
460
+ {id: 'font-display', weight: 0, group: 'diagnostics'},
461
+ {id: 'third-party-summary', weight: 0, group: 'diagnostics'},
462
+ {id: 'third-party-facades', weight: 0, group: 'diagnostics'},
463
+ {id: 'largest-contentful-paint-element', weight: 0, group: 'diagnostics'},
464
+ {id: 'lcp-lazy-loaded', weight: 0, group: 'diagnostics'},
465
+ {id: 'layout-shifts', weight: 0, group: 'diagnostics'},
466
+ {id: 'uses-passive-event-listeners', weight: 0, group: 'diagnostics'},
467
+ {id: 'no-document-write', weight: 0, group: 'diagnostics'},
468
+ {id: 'long-tasks', weight: 0, group: 'diagnostics'},
469
+ {id: 'non-composited-animations', weight: 0, group: 'diagnostics'},
470
+ {id: 'unsized-images', weight: 0, group: 'diagnostics'},
471
+ {id: 'viewport', weight: 0, group: 'diagnostics'},
472
+ {id: 'uses-responsive-images-snapshot', weight: 0, group: 'diagnostics'},
473
+ {id: 'work-during-interaction', weight: 0, group: 'diagnostics'},
474
+ {id: 'bf-cache', weight: 0, group: 'diagnostics'},
484
475
 
485
476
  // Budget audits.
486
477
  {id: 'performance-budget', weight: 0, group: 'budgets'},
@@ -23,7 +23,7 @@ const config = {
23
23
  // config is awkward - easier to omit the property here. Will defer to default config.
24
24
  'performance': {
25
25
  auditRefs: [
26
- {id: 'uses-rel-preload', weight: 0},
26
+ {id: 'uses-rel-preload', weight: 0, group: 'diagnostics'},
27
27
  ],
28
28
  },
29
29
  // @ts-ignore: `title` is required in CategoryJson. setting to the same value as the default
@@ -0,0 +1,79 @@
1
+ export type Node = import('../base-node.js').Node;
2
+ export type NetworkNode<T> = import('../network-node.js').NetworkNode<T>;
3
+ export type CpuNode = import('../cpu-node.js').CPUNode;
4
+ /** @typedef {import('../base-node.js').Node} Node */
5
+ /** @template T @typedef {import('../network-node.js').NetworkNode<T>} NetworkNode */
6
+ /** @typedef {import('../cpu-node.js').CPUNode} CpuNode */
7
+ export class FirstContentfulPaint extends Metric {
8
+ /**
9
+ * @template T
10
+ * @typedef FirstPaintBasedGraphOpts
11
+ * @property {number} cutoffTimestamp The timestamp used to filter out tasks that occured after
12
+ * our paint of interest. Typically this is First Contentful Paint or First Meaningful Paint.
13
+ * @property {function(NetworkNode<T>):boolean} treatNodeAsRenderBlocking The function that determines
14
+ * which resources should be considered *possibly* render-blocking.
15
+ * @property {(function(CpuNode):boolean)=} additionalCpuNodesToTreatAsRenderBlocking The function that
16
+ * determines which CPU nodes should also be included in our blocking node IDs set,
17
+ * beyond what getRenderBlockingNodeData() already includes.
18
+ */
19
+ /**
20
+ * This function computes the set of URLs that *appeared* to be render-blocking based on our filter,
21
+ * *but definitely were not* render-blocking based on the timing of their EvaluateScript task.
22
+ * It also computes the set of corresponding CPU node ids that were needed for the paint at the
23
+ * given timestamp.
24
+ *
25
+ * @template [T=unknown]
26
+ * @param {Node} graph
27
+ * @param {FirstPaintBasedGraphOpts<T>} opts
28
+ * @return {{definitelyNotRenderBlockingScriptUrls: Set<string>, renderBlockingCpuNodeIds: Set<string>}}
29
+ */
30
+ static getRenderBlockingNodeData<T = unknown>(graph: Node, { cutoffTimestamp, treatNodeAsRenderBlocking, additionalCpuNodesToTreatAsRenderBlocking }: {
31
+ /**
32
+ * The timestamp used to filter out tasks that occured after
33
+ * our paint of interest. Typically this is First Contentful Paint or First Meaningful Paint.
34
+ */
35
+ cutoffTimestamp: number;
36
+ /**
37
+ * The function that determines
38
+ * which resources should be considered *possibly* render-blocking.
39
+ */
40
+ treatNodeAsRenderBlocking: (arg0: import("../network-node.js").NetworkNode<T>) => boolean;
41
+ /**
42
+ * The function that
43
+ * determines which CPU nodes should also be included in our blocking node IDs set,
44
+ * beyond what getRenderBlockingNodeData() already includes.
45
+ */
46
+ additionalCpuNodesToTreatAsRenderBlocking?: ((arg0: CpuNode) => boolean) | undefined;
47
+ }): {
48
+ definitelyNotRenderBlockingScriptUrls: Set<string>;
49
+ renderBlockingCpuNodeIds: Set<string>;
50
+ };
51
+ /**
52
+ * This function computes the graph required for the first paint of interest.
53
+ *
54
+ * @template [T=unknown]
55
+ * @param {Node} dependencyGraph
56
+ * @param {FirstPaintBasedGraphOpts<T>} opts
57
+ * @return {Node}
58
+ */
59
+ static getFirstPaintBasedGraph<T_1 = unknown>(dependencyGraph: Node, { cutoffTimestamp, treatNodeAsRenderBlocking, additionalCpuNodesToTreatAsRenderBlocking }: {
60
+ /**
61
+ * The timestamp used to filter out tasks that occured after
62
+ * our paint of interest. Typically this is First Contentful Paint or First Meaningful Paint.
63
+ */
64
+ cutoffTimestamp: number;
65
+ /**
66
+ * The function that determines
67
+ * which resources should be considered *possibly* render-blocking.
68
+ */
69
+ treatNodeAsRenderBlocking: (arg0: import("../network-node.js").NetworkNode<T_1>) => boolean;
70
+ /**
71
+ * The function that
72
+ * determines which CPU nodes should also be included in our blocking node IDs set,
73
+ * beyond what getRenderBlockingNodeData() already includes.
74
+ */
75
+ additionalCpuNodesToTreatAsRenderBlocking?: ((arg0: CpuNode) => boolean) | undefined;
76
+ }): Node;
77
+ }
78
+ import { Metric } from '../metric.js';
79
+ //# sourceMappingURL=first-contentful-paint.d.ts.map
@@ -0,0 +1,200 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2024 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import * as Lantern from '../types/lantern.js';
8
+ import {Metric} from '../metric.js';
9
+ import {BaseNode} from '../base-node.js';
10
+
11
+ /** @typedef {import('../base-node.js').Node} Node */
12
+ /** @template T @typedef {import('../network-node.js').NetworkNode<T>} NetworkNode */
13
+ /** @typedef {import('../cpu-node.js').CPUNode} CpuNode */
14
+
15
+ class FirstContentfulPaint extends Metric {
16
+ /**
17
+ * @return {Lantern.Simulation.MetricCoefficients}
18
+ */
19
+ static get COEFFICIENTS() {
20
+ return {
21
+ intercept: 0,
22
+ optimistic: 0.5,
23
+ pessimistic: 0.5,
24
+ };
25
+ }
26
+
27
+ /**
28
+ * @template T
29
+ * @typedef FirstPaintBasedGraphOpts
30
+ * @property {number} cutoffTimestamp The timestamp used to filter out tasks that occured after
31
+ * our paint of interest. Typically this is First Contentful Paint or First Meaningful Paint.
32
+ * @property {function(NetworkNode<T>):boolean} treatNodeAsRenderBlocking The function that determines
33
+ * which resources should be considered *possibly* render-blocking.
34
+ * @property {(function(CpuNode):boolean)=} additionalCpuNodesToTreatAsRenderBlocking The function that
35
+ * determines which CPU nodes should also be included in our blocking node IDs set,
36
+ * beyond what getRenderBlockingNodeData() already includes.
37
+ */
38
+
39
+ /**
40
+ * This function computes the set of URLs that *appeared* to be render-blocking based on our filter,
41
+ * *but definitely were not* render-blocking based on the timing of their EvaluateScript task.
42
+ * It also computes the set of corresponding CPU node ids that were needed for the paint at the
43
+ * given timestamp.
44
+ *
45
+ * @template [T=unknown]
46
+ * @param {Node} graph
47
+ * @param {FirstPaintBasedGraphOpts<T>} opts
48
+ * @return {{definitelyNotRenderBlockingScriptUrls: Set<string>, renderBlockingCpuNodeIds: Set<string>}}
49
+ */
50
+ static getRenderBlockingNodeData(
51
+ graph,
52
+ {cutoffTimestamp, treatNodeAsRenderBlocking, additionalCpuNodesToTreatAsRenderBlocking}
53
+ ) {
54
+ /** @type {Map<string, CpuNode>} A map of blocking script URLs to the earliest EvaluateScript task node that executed them. */
55
+ const scriptUrlToNodeMap = new Map();
56
+
57
+ /** @type {Array<CpuNode>} */
58
+ const cpuNodes = [];
59
+ graph.traverse(node => {
60
+ if (node.type === BaseNode.TYPES.CPU) {
61
+ // A task is *possibly* render blocking if it *started* before cutoffTimestamp.
62
+ // We use startTime here because the paint event can be *inside* the task that was render blocking.
63
+ if (node.startTime <= cutoffTimestamp) cpuNodes.push(node);
64
+
65
+ // Build our script URL map to find the earliest EvaluateScript task node.
66
+ const scriptUrls = node.getEvaluateScriptURLs();
67
+ for (const url of scriptUrls) {
68
+ // Use the earliest CPU node we find.
69
+ const existing = scriptUrlToNodeMap.get(url) || node;
70
+ scriptUrlToNodeMap.set(url, node.startTime < existing.startTime ? node : existing);
71
+ }
72
+ }
73
+ });
74
+
75
+ cpuNodes.sort((a, b) => a.startTime - b.startTime);
76
+
77
+ // A script is *possibly* render blocking if it finished loading before cutoffTimestamp.
78
+ const possiblyRenderBlockingScriptUrls = Metric.getScriptUrls(graph, node => {
79
+ // The optimistic LCP treatNodeAsRenderBlocking fn wants to exclude some images in the graph,
80
+ // but here it only receives scripts to evaluate. It's a no-op in this case, but it will
81
+ // matter below in the getFirstPaintBasedGraph clone operation.
82
+ return node.endTime <= cutoffTimestamp && treatNodeAsRenderBlocking(node);
83
+ });
84
+
85
+ // A script is *definitely not* render blocking if its EvaluateScript task started after cutoffTimestamp.
86
+ /** @type {Set<string>} */
87
+ const definitelyNotRenderBlockingScriptUrls = new Set();
88
+ /** @type {Set<string>} */
89
+ const renderBlockingCpuNodeIds = new Set();
90
+ for (const url of possiblyRenderBlockingScriptUrls) {
91
+ // Lookup the CPU node that had the earliest EvaluateScript for this URL.
92
+ const cpuNodeForUrl = scriptUrlToNodeMap.get(url);
93
+
94
+ // If we can't find it at all, we can't conclude anything, so just skip it.
95
+ if (!cpuNodeForUrl) continue;
96
+
97
+ // If we found it and it was in our `cpuNodes` set that means it finished before cutoffTimestamp, so it really is render-blocking.
98
+ if (cpuNodes.includes(cpuNodeForUrl)) {
99
+ renderBlockingCpuNodeIds.add(cpuNodeForUrl.id);
100
+ continue;
101
+ }
102
+
103
+ // We couldn't find the evaluate script in the set of CPU nodes that ran before our paint, so
104
+ // it must not have been necessary for the paint.
105
+ definitelyNotRenderBlockingScriptUrls.add(url);
106
+ }
107
+
108
+ // The first layout, first paint, and first ParseHTML are almost always necessary for first paint,
109
+ // so we always include those CPU nodes.
110
+ const firstLayout = cpuNodes.find(node => node.didPerformLayout());
111
+ if (firstLayout) renderBlockingCpuNodeIds.add(firstLayout.id);
112
+ const firstPaint = cpuNodes.find(node => node.childEvents.some(e => e.name === 'Paint'));
113
+ if (firstPaint) renderBlockingCpuNodeIds.add(firstPaint.id);
114
+ const firstParse = cpuNodes.find(node => node.childEvents.some(e => e.name === 'ParseHTML'));
115
+ if (firstParse) renderBlockingCpuNodeIds.add(firstParse.id);
116
+
117
+ // If a CPU filter was passed in, we also want to include those extra nodes.
118
+ if (additionalCpuNodesToTreatAsRenderBlocking) {
119
+ cpuNodes
120
+ .filter(additionalCpuNodesToTreatAsRenderBlocking)
121
+ .forEach(node => renderBlockingCpuNodeIds.add(node.id));
122
+ }
123
+
124
+ return {
125
+ definitelyNotRenderBlockingScriptUrls,
126
+ renderBlockingCpuNodeIds,
127
+ };
128
+ }
129
+
130
+ /**
131
+ * This function computes the graph required for the first paint of interest.
132
+ *
133
+ * @template [T=unknown]
134
+ * @param {Node} dependencyGraph
135
+ * @param {FirstPaintBasedGraphOpts<T>} opts
136
+ * @return {Node}
137
+ */
138
+ static getFirstPaintBasedGraph(
139
+ dependencyGraph,
140
+ {cutoffTimestamp, treatNodeAsRenderBlocking, additionalCpuNodesToTreatAsRenderBlocking}
141
+ ) {
142
+ const rbData = this.getRenderBlockingNodeData(dependencyGraph, {
143
+ cutoffTimestamp,
144
+ treatNodeAsRenderBlocking,
145
+ additionalCpuNodesToTreatAsRenderBlocking,
146
+ });
147
+ const {definitelyNotRenderBlockingScriptUrls, renderBlockingCpuNodeIds} = rbData;
148
+
149
+ return dependencyGraph.cloneWithRelationships(node => {
150
+ if (node.type === BaseNode.TYPES.NETWORK) {
151
+ // Exclude all nodes that ended after cutoffTimestamp (except for the main document which we always consider necessary)
152
+ // endTime is negative if request does not finish, make sure startTime isn't after cutoffTimestamp in this case.
153
+ const endedAfterPaint = node.endTime > cutoffTimestamp || node.startTime > cutoffTimestamp;
154
+ if (endedAfterPaint && !node.isMainDocument()) return false;
155
+
156
+ const url = node.record.url;
157
+ // If the URL definitely wasn't render-blocking then we filter it out.
158
+ if (definitelyNotRenderBlockingScriptUrls.has(url)) {
159
+ return false;
160
+ }
161
+
162
+ // Lastly, build up the FCP graph of all nodes we consider render blocking
163
+ return treatNodeAsRenderBlocking(node);
164
+ } else {
165
+ // If it's a CPU node, just check if it was blocking.
166
+ return renderBlockingCpuNodeIds.has(node.id);
167
+ }
168
+ });
169
+ }
170
+
171
+ /**
172
+ * @param {Node} dependencyGraph
173
+ * @param {LH.Artifacts.ProcessedNavigation} processedNavigation
174
+ * @return {Node}
175
+ */
176
+ static getOptimisticGraph(dependencyGraph, processedNavigation) {
177
+ return this.getFirstPaintBasedGraph(dependencyGraph, {
178
+ cutoffTimestamp: processedNavigation.timestamps.firstContentfulPaint,
179
+ // In the optimistic graph we exclude resources that appeared to be render blocking but were
180
+ // initiated by a script. While they typically have a very high importance and tend to have a
181
+ // significant impact on the page's content, these resources don't technically block rendering.
182
+ treatNodeAsRenderBlocking: node =>
183
+ node.hasRenderBlockingPriority() && node.initiatorType !== 'script',
184
+ });
185
+ }
186
+
187
+ /**
188
+ * @param {Node} dependencyGraph
189
+ * @param {LH.Artifacts.ProcessedNavigation} processedNavigation
190
+ * @return {Node}
191
+ */
192
+ static getPessimisticGraph(dependencyGraph, processedNavigation) {
193
+ return this.getFirstPaintBasedGraph(dependencyGraph, {
194
+ cutoffTimestamp: processedNavigation.timestamps.firstContentfulPaint,
195
+ treatNodeAsRenderBlocking: node => node.hasRenderBlockingPriority(),
196
+ });
197
+ }
198
+ }
199
+
200
+ export {FirstContentfulPaint};
@@ -0,0 +1,6 @@
1
+ export type Node = import('../base-node.js').Node;
2
+ /** @typedef {import('../base-node.js').Node} Node */
3
+ export class FirstMeaningfulPaint extends Metric {
4
+ }
5
+ import { Metric } from '../metric.js';
6
+ //# sourceMappingURL=first-meaningful-paint.d.ts.map
@@ -0,0 +1,66 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2024 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import * as Lantern from '../types/lantern.js';
8
+ import {Metric} from '../metric.js';
9
+ // TODO(15841): don't use LighthouseError
10
+ import {LighthouseError} from '../../lh-error.js';
11
+ import {FirstContentfulPaint} from './first-contentful-paint.js';
12
+
13
+ /** @typedef {import('../base-node.js').Node} Node */
14
+
15
+ class FirstMeaningfulPaint extends Metric {
16
+ /**
17
+ * @return {Lantern.Simulation.MetricCoefficients}
18
+ */
19
+ static get COEFFICIENTS() {
20
+ return {
21
+ intercept: 0,
22
+ optimistic: 0.5,
23
+ pessimistic: 0.5,
24
+ };
25
+ }
26
+
27
+ /**
28
+ * @param {Node} dependencyGraph
29
+ * @param {LH.Artifacts.ProcessedNavigation} processedNavigation
30
+ * @return {Node}
31
+ */
32
+ static getOptimisticGraph(dependencyGraph, processedNavigation) {
33
+ const fmp = processedNavigation.timestamps.firstMeaningfulPaint;
34
+ if (!fmp) {
35
+ throw new LighthouseError(LighthouseError.errors.NO_FMP);
36
+ }
37
+ return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
38
+ cutoffTimestamp: fmp,
39
+ // See FirstContentfulPaint's getOptimisticGraph implementation for a longer description
40
+ // of why we exclude script initiated resources here.
41
+ treatNodeAsRenderBlocking: node =>
42
+ node.hasRenderBlockingPriority() && node.initiatorType !== 'script',
43
+ });
44
+ }
45
+
46
+ /**
47
+ * @param {Node} dependencyGraph
48
+ * @param {LH.Artifacts.ProcessedNavigation} processedNavigation
49
+ * @return {Node}
50
+ */
51
+ static getPessimisticGraph(dependencyGraph, processedNavigation) {
52
+ const fmp = processedNavigation.timestamps.firstMeaningfulPaint;
53
+ if (!fmp) {
54
+ throw new LighthouseError(LighthouseError.errors.NO_FMP);
55
+ }
56
+
57
+ return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
58
+ cutoffTimestamp: fmp,
59
+ treatNodeAsRenderBlocking: node => node.hasRenderBlockingPriority(),
60
+ // For pessimistic FMP we'll include *all* layout nodes
61
+ additionalCpuNodesToTreatAsRenderBlocking: node => node.didPerformLayout(),
62
+ });
63
+ }
64
+ }
65
+
66
+ export {FirstMeaningfulPaint};
@@ -0,0 +1,26 @@
1
+ export type Node = import('../base-node.js').Node;
2
+ export class Interactive extends Metric {
3
+ /**
4
+ * @param {Node} dependencyGraph
5
+ * @return {Node}
6
+ */
7
+ static getOptimisticGraph(dependencyGraph: Node): Node;
8
+ /**
9
+ * @param {Node} dependencyGraph
10
+ * @return {Node}
11
+ */
12
+ static getPessimisticGraph(dependencyGraph: Node): Node;
13
+ /**
14
+ * @param {LH.Gatherer.Simulation.Result} simulationResult
15
+ * @param {import('../metric.js').Extras} extras
16
+ * @return {LH.Gatherer.Simulation.Result}
17
+ */
18
+ static getEstimateFromSimulation(simulationResult: LH.Gatherer.Simulation.Result, extras: import('../metric.js').Extras): LH.Gatherer.Simulation.Result;
19
+ /**
20
+ * @param {LH.Gatherer.Simulation.Result['nodeTimings']} nodeTimings
21
+ * @return {number}
22
+ */
23
+ static getLastLongTaskEndTime(nodeTimings: LH.Gatherer.Simulation.Result['nodeTimings'], duration?: number): number;
24
+ }
25
+ import { Metric } from '../metric.js';
26
+ //# sourceMappingURL=interactive.d.ts.map