Skip to content

Commit bf5970b

Browse files
committed
Tidy obs.speedcurve.js
Normalise SpeedCurve data writes, reuse cached navigation data, and guard against a missing HEAD_End performance entry.
1 parent 81aca69 commit bf5970b

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

js/obs.speedcurve.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
*/
66

77
(() => {
8+
const lux = window.LUX;
9+
810
// 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;
1012

1113
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+
};
1219

1320
// Keys we intend to send. Keep in sync with obs.js
1421
const keys = [
@@ -31,55 +38,45 @@
3138

3239
for (const key of keys) {
3340
if (Object.prototype.hasOwnProperty.call(obs, key)) {
34-
window.LUX.addData(key, obs[key]);
41+
addData(key, obs[key]);
3542
}
3643
}
3744

38-
// Was the response from HTTP cache or the network?
39-
const navigation = performance.getEntriesByType('navigation')[0];
40-
4145
if (!navigation) return;
4246

47+
// Was the response from HTTP cache or the network?
4348
const { transferSize } = navigation;
4449

4550
if (transferSize === 0) {
46-
LUX.addData('fromCache', true);
51+
addData('fromCache', true);
4752
} else if (transferSize > 0) {
48-
LUX.addData('fromCache', false);
53+
addData('fromCache', false);
4954
}
5055

5156
// Was the response from the back–forward cache?
5257
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);
5859
});
5960

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);
6563
}
6664

6765
// Time to Last Byte (TTLB)
6866
if (navigation.responseEnd && navigation.startTime >= 0) {
6967
const ttlb = Math.round(navigation.responseEnd - navigation.startTime);
7068
if (Number.isFinite(ttlb) && ttlb >= 0) {
71-
LUX.addData('ttlb', ttlb);
69+
addData('ttlb', ttlb);
7270
}
7371
}
7472

7573
// 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) {
7877
const fpp = Math.round(headEnd.startTime - navigation.startTime);
7978
if (Number.isFinite(fpp) && fpp >= 0) {
80-
LUX.addData('fpp', fpp);
79+
addData('fpp', fpp);
8180
}
8281
}
83-
84-
8582
})();

0 commit comments

Comments
 (0)