getCLS.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { bindReporter } from './lib/bindReporter.js';
  2. import { initMetric } from './lib/initMetric.js';
  3. import { observe } from './lib/observe.js';
  4. import { onHidden } from './lib/onHidden.js';
  5. /*
  6. * Copyright 2020 Google LLC
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * https://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * Calculates the [CLS](https://web.dev/cls/) value for the current page and
  22. * calls the `callback` function once the value is ready to be reported, along
  23. * with all `layout-shift` performance entries that were used in the metric
  24. * value calculation. The reported value is a `double` (corresponding to a
  25. * [layout shift score](https://web.dev/cls/#layout-shift-score)).
  26. *
  27. * If the `reportAllChanges` configuration option is set to `true`, the
  28. * `callback` function will be called as soon as the value is initially
  29. * determined as well as any time the value changes throughout the page
  30. * lifespan.
  31. *
  32. * _**Important:** CLS should be continually monitored for changes throughout
  33. * the entire lifespan of a page—including if the user returns to the page after
  34. * it's been hidden/backgrounded. However, since browsers often [will not fire
  35. * additional callbacks once the user has backgrounded a
  36. * page](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden),
  37. * `callback` is always called when the page's visibility state changes to
  38. * hidden. As a result, the `callback` function might be called multiple times
  39. * during the same page load._
  40. */
  41. const onCLS = (onReport) => {
  42. const metric = initMetric('CLS', 0);
  43. let report;
  44. let sessionValue = 0;
  45. let sessionEntries = [];
  46. // const handleEntries = (entries: Metric['entries']) => {
  47. const handleEntries = (entries) => {
  48. entries.forEach(entry => {
  49. // Only count layout shifts without recent user input.
  50. if (!entry.hadRecentInput) {
  51. const firstSessionEntry = sessionEntries[0];
  52. const lastSessionEntry = sessionEntries[sessionEntries.length - 1];
  53. // If the entry occurred less than 1 second after the previous entry and
  54. // less than 5 seconds after the first entry in the session, include the
  55. // entry in the current session. Otherwise, start a new session.
  56. if (
  57. sessionValue &&
  58. sessionEntries.length !== 0 &&
  59. entry.startTime - lastSessionEntry.startTime < 1000 &&
  60. entry.startTime - firstSessionEntry.startTime < 5000
  61. ) {
  62. sessionValue += entry.value;
  63. sessionEntries.push(entry);
  64. } else {
  65. sessionValue = entry.value;
  66. sessionEntries = [entry];
  67. }
  68. // If the current session value is larger than the current CLS value,
  69. // update CLS and the entries contributing to it.
  70. if (sessionValue > metric.value) {
  71. metric.value = sessionValue;
  72. metric.entries = sessionEntries;
  73. if (report) {
  74. report();
  75. }
  76. }
  77. }
  78. });
  79. };
  80. const po = observe('layout-shift', handleEntries);
  81. if (po) {
  82. report = bindReporter(onReport, metric);
  83. const stopListening = () => {
  84. handleEntries(po.takeRecords() );
  85. report(true);
  86. };
  87. onHidden(stopListening);
  88. return stopListening;
  89. }
  90. return;
  91. };
  92. export { onCLS };
  93. //# sourceMappingURL=getCLS.js.map