UNPKG

3.32 kBPlain TextView Raw
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
17import {onBFCacheRestore} from './lib/bfcache.js';
18import {bindReporter} from './lib/bindReporter.js';
19import {getVisibilityWatcher} from './lib/getVisibilityWatcher.js';
20import {initMetric} from './lib/initMetric.js';
21import {observe} from './lib/observe.js';
22import {onHidden} from './lib/onHidden.js';
23import {
24 firstInputPolyfill,
25 resetFirstInputPolyfill,
26} from './lib/polyfills/firstInputPolyfill.js';
27import {runOnce} from './lib/runOnce.js';
28import {whenActivated} from './lib/whenActivated.js';
29import {
30 FIDMetric,
31 FirstInputPolyfillCallback,
32 MetricRatingThresholds,
33 ReportOpts,
34} from './types.js';
35
36/** Thresholds for FID. See https://web.dev/articles/fid#what_is_a_good_fid_score */
37export const FIDThresholds: MetricRatingThresholds = [100, 300];
38
39/**
40 * Calculates the [FID](https://web.dev/articles/fid) value for the current page and
41 * calls the `callback` function once the value is ready, along with the
42 * relevant `first-input` performance entry used to determine the value. The
43 * reported value is a `DOMHighResTimeStamp`.
44 *
45 * _**Important:** since FID is only reported after the user interacts with the
46 * page, it's possible that it will not be reported for some page loads._
47 */
48export const onFID = (
49 onReport: (metric: FIDMetric) => void,
50 opts?: ReportOpts,
51) => {
52 // Set defaults
53 opts = opts || {};
54
55 whenActivated(() => {
56 const visibilityWatcher = getVisibilityWatcher();
57 let metric = initMetric('FID');
58 let report: ReturnType<typeof bindReporter>;
59
60 const handleEntry = (entry: PerformanceEventTiming) => {
61 // Only report if the page wasn't hidden prior to the first input.
62 if (entry.startTime < visibilityWatcher.firstHiddenTime) {
63 metric.value = entry.processingStart - entry.startTime;
64 metric.entries.push(entry);
65 report(true);
66 }
67 };
68
69 const handleEntries = (entries: FIDMetric['entries']) => {
70 entries.forEach(handleEntry);
71 };
72
73 const po = observe('first-input', handleEntries);
74
75 report = bindReporter(
76 onReport,
77 metric,
78 FIDThresholds,
79 opts!.reportAllChanges,
80 );
81
82 if (po) {
83 onHidden(
84 runOnce(() => {
85 handleEntries(po.takeRecords() as FIDMetric['entries']);
86 po.disconnect();
87 }),
88 );
89
90 onBFCacheRestore(() => {
91 metric = initMetric('FID');
92 report = bindReporter(
93 onReport,
94 metric,
95 FIDThresholds,
96 opts!.reportAllChanges,
97 );
98
99 // Browsers don't re-emit FID on bfcache restore so fake it until you make it
100 resetFirstInputPolyfill();
101 firstInputPolyfill(handleEntry as FirstInputPolyfillCallback);
102 });
103 }
104 });
105};