|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { bindReporter } from './lib/bindReporter'; |
| 18 | +import { initMetric } from './lib/initMetric'; |
| 19 | +import { observe, PerformanceEntryHandler } from './lib/observe'; |
| 20 | +import { onHidden } from './lib/onHidden'; |
| 21 | +import { ReportHandler } from './types'; |
| 22 | + |
| 23 | +// https://wicg.github.io/layout-instability/#sec-layout-shift |
| 24 | +export interface LayoutShift extends PerformanceEntry { |
| 25 | + value: number; |
| 26 | + hadRecentInput: boolean; |
| 27 | + sources: Array<LayoutShiftAttribution>; |
| 28 | + toJSON(): Record<string, unknown>; |
| 29 | +} |
| 30 | + |
| 31 | +export interface LayoutShiftAttribution { |
| 32 | + node?: Node; |
| 33 | + previousRect: DOMRectReadOnly; |
| 34 | + currentRect: DOMRectReadOnly; |
| 35 | +} |
| 36 | + |
| 37 | +export const getUpdatedCLS = (onReport: ReportHandler, reportAllChanges?: boolean): void => { |
| 38 | + const metric = initMetric('UpdatedCLS', 0); |
| 39 | + let report: ReturnType<typeof bindReporter>; |
| 40 | + |
| 41 | + let sessionValue = 0; |
| 42 | + let sessionEntries: PerformanceEntry[] = []; |
| 43 | + |
| 44 | + const entryHandler = (entry: LayoutShift): void => { |
| 45 | + // Only count layout shifts without recent user input. |
| 46 | + if (!entry.hadRecentInput) { |
| 47 | + const firstSessionEntry = sessionEntries[0]; |
| 48 | + const lastSessionEntry = sessionEntries[sessionEntries.length - 1]; |
| 49 | + |
| 50 | + // If the entry occurred less than 1 second after the previous entry and |
| 51 | + // less than 5 seconds after the first entry in the session, include the |
| 52 | + // entry in the current session. Otherwise, start a new session. |
| 53 | + if ( |
| 54 | + sessionValue && |
| 55 | + entry.startTime - lastSessionEntry.startTime < 1000 && |
| 56 | + entry.startTime - firstSessionEntry.startTime < 5000 |
| 57 | + ) { |
| 58 | + sessionValue += entry.value; |
| 59 | + sessionEntries.push(entry); |
| 60 | + } else { |
| 61 | + sessionValue = entry.value; |
| 62 | + sessionEntries = [entry]; |
| 63 | + } |
| 64 | + |
| 65 | + // If the current session value is larger than the current CLS value, |
| 66 | + // update CLS and the entries contributing to it. |
| 67 | + if (sessionValue > metric.value) { |
| 68 | + metric.value = sessionValue; |
| 69 | + metric.entries = sessionEntries; |
| 70 | + if (report) { |
| 71 | + report(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const po = observe('layout-shift', entryHandler as PerformanceEntryHandler); |
| 78 | + if (po) { |
| 79 | + report = bindReporter(onReport, metric, reportAllChanges); |
| 80 | + |
| 81 | + onHidden(() => { |
| 82 | + po.takeRecords().map(entryHandler as PerformanceEntryHandler); |
| 83 | + report(true); |
| 84 | + }); |
| 85 | + } |
| 86 | +}; |
0 commit comments