|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | (() => { |
| 8 | + const lux = window.LUX; |
| 9 | + |
8 | 10 | // Bail out if SpeedCurve is not available. |
9 | | - if (!window.LUX || typeof window.LUX.addData !== 'function') return; |
| 11 | + if (!lux || typeof lux.addData !== 'function') return; |
10 | 12 |
|
11 | 13 | const obs = window.obs || Object.create(null); |
| 14 | + const navigation = performance.getEntriesByType('navigation')[0]; |
| 15 | + |
| 16 | + const addData = (key, value) => { |
| 17 | + lux.addData(key, value); |
| 18 | + }; |
12 | 19 |
|
13 | 20 | // Keys we intend to send. Keep in sync with obs.js |
14 | 21 | const keys = [ |
|
31 | 38 |
|
32 | 39 | for (const key of keys) { |
33 | 40 | if (Object.prototype.hasOwnProperty.call(obs, key)) { |
34 | | - window.LUX.addData(key, obs[key]); |
| 41 | + addData(key, obs[key]); |
35 | 42 | } |
36 | 43 | } |
37 | 44 |
|
38 | | - // Was the response from HTTP cache or the network? |
39 | | - const navigation = performance.getEntriesByType('navigation')[0]; |
40 | | - |
41 | 45 | if (!navigation) return; |
42 | 46 |
|
| 47 | + // Was the response from HTTP cache or the network? |
43 | 48 | const { transferSize } = navigation; |
44 | 49 |
|
45 | 50 | if (transferSize === 0) { |
46 | | - LUX.addData('fromCache', true); |
| 51 | + addData('fromCache', true); |
47 | 52 | } else if (transferSize > 0) { |
48 | | - LUX.addData('fromCache', false); |
| 53 | + addData('fromCache', false); |
49 | 54 | } |
50 | 55 |
|
51 | 56 | // Was the response from the back–forward cache? |
52 | 57 | window.addEventListener('pageshow', (event) => { |
53 | | - if (event.persisted) { |
54 | | - LUX.addData('frombfCache', true); |
55 | | - } else { |
56 | | - LUX.addData('frombfCache', false); |
57 | | - } |
| 58 | + addData('frombfCache', event.persisted); |
58 | 59 | }); |
59 | 60 |
|
60 | | - if (document.prerendering) { |
61 | | - } else if (performance.getEntriesByType("navigation")[0]?.activationStart > 0) { |
62 | | - LUX.addData('fromPrerender', true); |
63 | | - } else { |
64 | | - LUX.addData('fromPrerender', false); |
| 61 | + if (!document.prerendering) { |
| 62 | + addData('fromPrerender', navigation.activationStart > 0); |
65 | 63 | } |
66 | 64 |
|
67 | 65 | // Time to Last Byte (TTLB) |
68 | 66 | if (navigation.responseEnd && navigation.startTime >= 0) { |
69 | 67 | const ttlb = Math.round(navigation.responseEnd - navigation.startTime); |
70 | 68 | if (Number.isFinite(ttlb) && ttlb >= 0) { |
71 | | - LUX.addData('ttlb', ttlb); |
| 69 | + addData('ttlb', ttlb); |
72 | 70 | } |
73 | 71 | } |
74 | 72 |
|
75 | 73 | // First Potential Paint (FPP) |
76 | | - if (navigation.startTime >= 0) { |
77 | | - const headEnd = performance.getEntriesByName('HEAD_End')[0]; |
| 74 | + const headEnd = performance.getEntriesByName('HEAD_End')[0]; |
| 75 | + |
| 76 | + if (headEnd && navigation.startTime >= 0) { |
78 | 77 | const fpp = Math.round(headEnd.startTime - navigation.startTime); |
79 | 78 | if (Number.isFinite(fpp) && fpp >= 0) { |
80 | | - LUX.addData('fpp', fpp); |
| 79 | + addData('fpp', fpp); |
81 | 80 | } |
82 | 81 | } |
83 | | - |
84 | | - |
85 | 82 | })(); |
0 commit comments