1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {getBFCacheRestoreTime} from './bfcache.js';
|
18 | import {generateUniqueID} from './generateUniqueID.js';
|
19 | import {getActivationStart} from './getActivationStart.js';
|
20 | import {getNavigationEntry} from './getNavigationEntry.js';
|
21 | import {Metric} from '../types.js';
|
22 |
|
23 | export const initMetric = (name: Metric['name'], value?: number): Metric => {
|
24 | const navEntry = getNavigationEntry();
|
25 | let navigationType: Metric['navigationType'] = 'navigate';
|
26 |
|
27 | if (getBFCacheRestoreTime() >= 0) {
|
28 | navigationType = 'back-forward-cache';
|
29 | } else if (navEntry) {
|
30 | if (document.prerendering || getActivationStart() > 0) {
|
31 | navigationType = 'prerender';
|
32 | } else if (document.wasDiscarded) {
|
33 | navigationType = 'restore';
|
34 | } else {
|
35 | navigationType = navEntry.type.replace(
|
36 | /_/g,
|
37 | '-'
|
38 | ) as Metric['navigationType'];
|
39 | }
|
40 | }
|
41 |
|
42 | return {
|
43 | name,
|
44 | value: typeof value === 'undefined' ? -1 : value,
|
45 | rating: 'good',
|
46 | delta: 0,
|
47 | entries: [],
|
48 | id: generateUniqueID(),
|
49 | navigationType,
|
50 | };
|
51 | };
|