getCLS.js 3.8 KB

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