UNPKG

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