1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { bindReporter } from './lib/bindReporter.js';
|
17 | import { finalMetrics } from './lib/finalMetrics.js';
|
18 | import { getFirstHidden } from './lib/getFirstHidden.js';
|
19 | import { initMetric } from './lib/initMetric.js';
|
20 | import { observe } from './lib/observe.js';
|
21 | import { onBFCacheRestore } from './lib/onBFCacheRestore.js';
|
22 | export const getFCP = (onReport, reportAllChanges) => {
|
23 | const firstHidden = getFirstHidden();
|
24 | let metric = initMetric('FCP');
|
25 | let report;
|
26 | const entryHandler = (entry) => {
|
27 | if (entry.name === 'first-contentful-paint') {
|
28 | if (po) {
|
29 | po.disconnect();
|
30 | }
|
31 |
|
32 | if (entry.startTime < firstHidden.timeStamp) {
|
33 | metric.value = entry.startTime;
|
34 | metric.entries.push(entry);
|
35 | finalMetrics.add(metric);
|
36 | report();
|
37 | }
|
38 | }
|
39 | };
|
40 | const po = observe('paint', entryHandler);
|
41 | if (po) {
|
42 | report = bindReporter(onReport, metric, reportAllChanges);
|
43 | onBFCacheRestore((event) => {
|
44 | metric = initMetric('FCP');
|
45 | report = bindReporter(onReport, metric, reportAllChanges);
|
46 | requestAnimationFrame(() => {
|
47 | requestAnimationFrame(() => {
|
48 | metric.value = performance.now() - event.timeStamp;
|
49 | finalMetrics.add(metric);
|
50 | report();
|
51 | });
|
52 | });
|
53 | });
|
54 | }
|
55 | };
|