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 | export interface Metric {
|
18 | // The name of the metric (in acronym form).
|
19 | name: 'CLS' | 'FCP' | 'FID' | 'LCP' | 'TTFB';
|
20 |
|
21 | // The current value of the metric.
|
22 | value: number;
|
23 |
|
24 | // The delta between the current value and the last-reported value.
|
25 | // On the first report, `delta` and `value` will always be the same.
|
26 | delta: number;
|
27 |
|
28 | // A unique ID representing this particular metric instance. This ID can
|
29 | // be used by an analytics tool to dedupe multiple values sent for the same
|
30 | // metric instance, or to group multiple deltas together and calculate a
|
31 | // total. It can also be used to differentiate multiple different metric
|
32 | // instances sent from the same page, which can happen if the page is
|
33 | // restored from the back/forward cache (in that case new metrics object
|
34 | // get created).
|
35 | id: string;
|
36 |
|
37 | // Any performance entries used in the metric value calculation.
|
38 | // Note, entries will be added to the array as the value changes.
|
39 | entries: (PerformanceEntry | FirstInputPolyfillEntry | NavigationTimingPolyfillEntry)[];
|
40 | }
|
41 |
|
42 | export interface ReportHandler {
|
43 | (metric: Metric): void;
|
44 | }
|
45 |
|
46 | // https://wicg.github.io/event-timing/#sec-performance-event-timing
|
47 | export interface PerformanceEventTiming extends PerformanceEntry {
|
48 | processingStart: DOMHighResTimeStamp;
|
49 | processingEnd: DOMHighResTimeStamp;
|
50 | duration: DOMHighResTimeStamp;
|
51 | cancelable?: boolean;
|
52 | target?: Element;
|
53 | }
|
54 |
|
55 | export type FirstInputPolyfillEntry =
|
56 | Omit<PerformanceEventTiming, 'processingEnd' | 'toJSON'>
|
57 |
|
58 | export interface FirstInputPolyfillCallback {
|
59 | (entry: FirstInputPolyfillEntry): void;
|
60 | }
|
61 |
|
62 | export type NavigationTimingPolyfillEntry = Omit<PerformanceNavigationTiming,
|
63 | 'initiatorType' | 'nextHopProtocol' | 'redirectCount' | 'transferSize' |
|
64 | 'encodedBodySize' | 'decodedBodySize' | 'toJSON'>
|
65 |
|
66 | export interface WebVitalsGlobal {
|
67 | firstInputPolyfill: (onFirstInput: FirstInputPolyfillCallback) => void;
|
68 | resetFirstInputPolyfill: () => void;
|
69 | firstHiddenTime: number;
|
70 | }
|
71 |
|
72 | declare global {
|
73 | interface Window {
|
74 | webVitals: WebVitalsGlobal;
|
75 |
|
76 | // Build flags:
|
77 | __WEB_VITALS_POLYFILL__: boolean;
|
78 | }
|
79 | }
|