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