UNPKG

3.33 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 {doubleRAF} from './lib/doubleRAF.js';
20import {getActivationStart} from './lib/getActivationStart.js';
21import {getVisibilityWatcher} from './lib/getVisibilityWatcher.js';
22import {initMetric} from './lib/initMetric.js';
23import {observe} from './lib/observe.js';
24import {whenActivated} from './lib/whenActivated.js';
25import {FCPMetric, MetricRatingThresholds, ReportOpts} from './types.js';
26
27/** Thresholds for FCP. See https://web.dev/articles/fcp#what_is_a_good_fcp_score */
28export const FCPThresholds: MetricRatingThresholds = [1800, 3000];
29
30/**
31 * Calculates the [FCP](https://web.dev/articles/fcp) value for the current page and
32 * calls the `callback` function once the value is ready, along with the
33 * relevant `paint` performance entry used to determine the value. The reported
34 * value is a `DOMHighResTimeStamp`.
35 */
36export const onFCP = (
37 onReport: (metric: FCPMetric) => void,
38 opts?: ReportOpts,
39) => {
40 // Set defaults
41 opts = opts || {};
42
43 whenActivated(() => {
44 const visibilityWatcher = getVisibilityWatcher();
45 let metric = initMetric('FCP');
46 let report: ReturnType<typeof bindReporter>;
47
48 const handleEntries = (entries: FCPMetric['entries']) => {
49 entries.forEach((entry) => {
50 if (entry.name === 'first-contentful-paint') {
51 po!.disconnect();
52
53 // Only report if the page wasn't hidden prior to the first paint.
54 if (entry.startTime < visibilityWatcher.firstHiddenTime) {
55 // The activationStart reference is used because FCP should be
56 // relative to page activation rather than navigation start if the
57 // page was prerendered. But in cases where `activationStart` occurs
58 // after the FCP, this time should be clamped at 0.
59 metric.value = Math.max(entry.startTime - getActivationStart(), 0);
60 metric.entries.push(entry);
61 report(true);
62 }
63 }
64 });
65 };
66
67 const po = observe('paint', handleEntries);
68
69 if (po) {
70 report = bindReporter(
71 onReport,
72 metric,
73 FCPThresholds,
74 opts!.reportAllChanges,
75 );
76
77 // Only report after a bfcache restore if the `PerformanceObserver`
78 // successfully registered or the `paint` entry exists.
79 onBFCacheRestore((event) => {
80 metric = initMetric('FCP');
81 report = bindReporter(
82 onReport,
83 metric,
84 FCPThresholds,
85 opts!.reportAllChanges,
86 );
87
88 doubleRAF(() => {
89 metric.value = performance.now() - event.timeStamp;
90 report(true);
91 });
92 });
93 }
94 });
95};