1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {getBFCacheRestoreTime} from '../lib/bfcache.js';
|
18 | import {getLoadState} from '../lib/getLoadState.js';
|
19 | import {getNavigationEntry} from '../lib/getNavigationEntry.js';
|
20 | import {onFCP as unattributedOnFCP} from '../onFCP.js';
|
21 | import {
|
22 | FCPMetric,
|
23 | FCPMetricWithAttribution,
|
24 | FCPReportCallback,
|
25 | FCPReportCallbackWithAttribution,
|
26 | ReportOpts,
|
27 | } from '../types.js';
|
28 |
|
29 | const attributeFCP = (metric: FCPMetric): void => {
|
30 | if (metric.entries.length) {
|
31 | const navigationEntry = getNavigationEntry();
|
32 | const fcpEntry = metric.entries[metric.entries.length - 1];
|
33 |
|
34 | if (navigationEntry) {
|
35 | const activationStart = navigationEntry.activationStart || 0;
|
36 | const ttfb = Math.max(0, navigationEntry.responseStart - activationStart);
|
37 |
|
38 | (metric as FCPMetricWithAttribution).attribution = {
|
39 | timeToFirstByte: ttfb,
|
40 | firstByteToFCP: metric.value - ttfb,
|
41 | loadState: getLoadState(metric.entries[0].startTime),
|
42 | navigationEntry,
|
43 | fcpEntry,
|
44 | };
|
45 | return;
|
46 | }
|
47 | }
|
48 |
|
49 | (metric as FCPMetricWithAttribution).attribution = {
|
50 | timeToFirstByte: 0,
|
51 | firstByteToFCP: metric.value,
|
52 | loadState: getLoadState(getBFCacheRestoreTime()),
|
53 | };
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export const onFCP = (
|
63 | onReport: FCPReportCallbackWithAttribution,
|
64 | opts?: ReportOpts
|
65 | ) => {
|
66 | unattributedOnFCP(
|
67 | ((metric: FCPMetricWithAttribution) => {
|
68 | attributeFCP(metric);
|
69 | onReport(metric);
|
70 | }) as FCPReportCallback,
|
71 | opts
|
72 | );
|
73 | };
|