Skip to content

Commit 9637bcc

Browse files
committed
feat(beacon): TTFB DNS metric is added
1 parent 902cdd5 commit 9637bcc

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

lib/payload.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface TimeToFirstByte extends Metric {
3333
waiting: number,
3434
cache: number,
3535
connection: number,
36+
dns: number,
3637
request: number,
3738
isCached: boolean,
3839
serverTiming: SimpleDictionary

lib/time-to-first-byte.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export default function(payload: Payload) {
1010
const ttfb: TimeToFirstByte = {
1111
value: metric.value,
1212
cache: metric.attribution.cacheDuration,
13-
connection: metric.attribution.connectionDuration ?? 0,
14-
request: metric.attribution.requestDuration ?? 0,
15-
waiting: metric.attribution.waitingDuration ?? 0,
13+
dns: metric.attribution.dnsDuration,
14+
connection: metric.attribution.connectionDuration,
15+
request: metric.attribution.requestDuration,
16+
waiting: metric.attribution.waitingDuration,
1617
isCached: false,
1718
navigationType: metric.navigationType,
1819
serverTiming: {}

src/view/frontend/web/js/beacon.js

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ const st = () => document.visibilityState !== "hidden" || document.prerendering
153153
const u = w(o, ot), g = (h) => {
154154
for (const T of h) u.u(T);
155155
u.o > s.value && (s.value = u.o, s.entries = u.i, a());
156-
}, m = x("layout-shift", g);
157-
m && (a = b(n, s, lt, o.reportAllChanges), document.addEventListener("visibilitychange", () => {
158-
document.visibilityState === "hidden" && (g(m.takeRecords()), a(!0));
156+
}, p = x("layout-shift", g);
157+
p && (a = b(n, s, lt, o.reportAllChanges), document.addEventListener("visibilitychange", () => {
158+
document.visibilityState === "hidden" && (g(p.takeRecords()), a(!0));
159159
}), P(() => {
160160
u.o = 0, s = S("CLS", 0), a = b(n, s, lt, o.reportAllChanges), tt(() => a());
161161
}), setTimeout(a));
@@ -164,7 +164,7 @@ const st = () => document.visibilityState !== "hidden" || document.prerendering
164164
const o = ((a) => {
165165
let s = {};
166166
if (a.entries.length) {
167-
const u = a.entries.reduce((g, m) => g.value > m.value ? g : m);
167+
const u = a.entries.reduce((g, p) => g.value > p.value ? g : p);
168168
if (u?.sources?.length) {
169169
const g = dt(u.sources);
170170
g && (s = { largestShiftTarget: i.get(g), largestShiftTime: u.startTime, largestShiftValue: u.value, largestShiftSource: g, largestShiftEntry: u, loadState: R(u.startTime) });
@@ -199,7 +199,7 @@ const gt = () => K ? Dt : performance.interactionCount ?? 0, kt = () => {
199199
"interactionCount" in performance || K || (K = x("event", Ft, { type: "event", buffered: !0, durationThreshold: 0 }));
200200
};
201201
let ft = 0;
202-
class mt {
202+
class pt {
203203
l = [];
204204
h = /* @__PURE__ */ new Map();
205205
m;
@@ -229,32 +229,32 @@ const X = (e) => {
229229
document.visibilityState === "hidden" ? e() : (e = et(e), document.addEventListener("visibilitychange", e, { once: !0 }), t(() => {
230230
e(), document.removeEventListener("visibilitychange", e);
231231
}));
232-
}, pt = [200, 500], qt = (e, t = {}) => {
233-
const r = w(t = Object.assign({}, t), mt);
232+
}, mt = [200, 500], qt = (e, t = {}) => {
233+
const r = w(t = Object.assign({}, t), pt);
234234
let i = [], n = [], o = 0;
235235
const a = /* @__PURE__ */ new WeakMap(), s = /* @__PURE__ */ new WeakMap();
236236
let u = !1;
237237
const g = () => {
238-
u || (X(m), u = !0);
239-
}, m = () => {
240-
const c = r.l.map((p) => a.get(p.entries[0])), l = n.length - 50;
241-
n = n.filter((p, y) => y >= l || c.includes(p));
238+
u || (X(p), u = !0);
239+
}, p = () => {
240+
const c = r.l.map((m) => a.get(m.entries[0])), l = n.length - 50;
241+
n = n.filter((m, y) => y >= l || c.includes(m));
242242
const d = /* @__PURE__ */ new Set();
243-
for (const p of n) {
244-
const y = h(p.startTime, p.processingEnd);
243+
for (const m of n) {
244+
const y = h(m.startTime, m.processingEnd);
245245
for (const C of y) d.add(C);
246246
}
247247
const f = i.length - 1 - 50;
248-
i = i.filter((p, y) => p.startTime > o && y > f || d.has(p)), u = !1;
248+
i = i.filter((m, y) => m.startTime > o && y > f || d.has(m)), u = !1;
249249
};
250250
r.m = (c) => {
251251
const l = c.startTime + c.duration;
252252
let d;
253253
o = Math.max(o, c.processingEnd);
254254
for (let f = n.length - 1; f >= 0; f--) {
255-
const p = n[f];
256-
if (Math.abs(l - p.renderTime) <= 8) {
257-
d = p, d.startTime = Math.min(c.startTime, d.startTime), d.processingStart = Math.min(c.processingStart, d.processingStart), d.processingEnd = Math.max(c.processingEnd, d.processingEnd), d.entries.push(c);
255+
const m = n[f];
256+
if (Math.abs(l - m.renderTime) <= 8) {
257+
d = m, d.startTime = Math.min(c.startTime, d.startTime), d.processingStart = Math.min(c.processingStart, d.processingStart), d.processingEnd = Math.max(c.processingEnd, d.processingEnd), d.entries.push(c);
258258
break;
259259
}
260260
}
@@ -273,7 +273,7 @@ const X = (e) => {
273273
}
274274
return d;
275275
}, T = (c) => {
276-
const l = c.entries[0], d = a.get(l), f = l.processingStart, p = Math.max(l.startTime + l.duration, f), y = Math.min(d.processingEnd, p), C = d.entries.sort((v, D) => v.processingStart - D.processingStart), N = h(l.startTime, y), L = r.h.get(l.interactionId), k = { interactionTarget: s.get(L), interactionType: l.name.startsWith("key") ? "keyboard" : "pointer", interactionTime: l.startTime, nextPaintTime: p, processedEventEntries: C, longAnimationFrameEntries: N, inputDelay: f - l.startTime, processingDuration: y - f, presentationDelay: p - y, loadState: R(l.startTime), longestScript: void 0, totalScriptDuration: void 0, totalStyleAndLayoutDuration: void 0, totalPaintDuration: void 0, totalUnattributedDuration: void 0 };
276+
const l = c.entries[0], d = a.get(l), f = l.processingStart, m = Math.max(l.startTime + l.duration, f), y = Math.min(d.processingEnd, m), C = d.entries.sort((v, D) => v.processingStart - D.processingStart), N = h(l.startTime, y), L = r.h.get(l.interactionId), k = { interactionTarget: s.get(L), interactionType: l.name.startsWith("key") ? "keyboard" : "pointer", interactionTime: l.startTime, nextPaintTime: m, processedEventEntries: C, longAnimationFrameEntries: N, inputDelay: f - l.startTime, processingDuration: y - f, presentationDelay: m - y, loadState: R(l.startTime), longestScript: void 0, totalScriptDuration: void 0, totalStyleAndLayoutDuration: void 0, totalPaintDuration: void 0, totalUnattributedDuration: void 0 };
277277
return ((v) => {
278278
if (!v.longAnimationFrameEntries?.length) return;
279279
const D = v.interactionTime, _ = v.inputDelay, nt = v.processingDuration;
@@ -297,17 +297,17 @@ const X = (e) => {
297297
globalThis.PerformanceEventTiming && "interactionId" in PerformanceEventTiming.prototype && U(() => {
298298
kt();
299299
let d, f = S("INP");
300-
const p = w(l, mt), y = (N) => {
300+
const m = w(l, pt), y = (N) => {
301301
X(() => {
302-
for (const k of N) p.u(k);
303-
const L = p.M();
302+
for (const k of N) m.u(k);
303+
const L = m.M();
304304
L && L.T !== f.value && (f.value = L.T, f.entries = L.entries, d());
305305
});
306306
}, C = x("event", y, { durationThreshold: l.durationThreshold ?? 40 });
307-
d = b(c, f, pt, l.reportAllChanges), C && (C.observe({ type: "first-input", buffered: !0 }), document.addEventListener("visibilitychange", () => {
307+
d = b(c, f, mt, l.reportAllChanges), C && (C.observe({ type: "first-input", buffered: !0 }), document.addEventListener("visibilitychange", () => {
308308
document.visibilityState === "hidden" && (y(C.takeRecords()), d(!0));
309309
}), P(() => {
310-
p.v(), f = S("INP"), d = b(c, f, pt, l.reportAllChanges);
310+
m.v(), f = S("INP"), d = b(c, f, mt, l.reportAllChanges);
311311
}));
312312
});
313313
})((c) => {
@@ -332,14 +332,14 @@ const vt = [2500, 4e3], Bt = (e, t = {}) => {
332332
U(() => {
333333
const a = St();
334334
let s, u = S("LCP");
335-
const g = w(o, ht), m = (T) => {
335+
const g = w(o, ht), p = (T) => {
336336
o.reportAllChanges || (T = T.slice(-1));
337337
for (const c of T) g.u(c), c.startTime < a.firstHiddenTime && (u.value = Math.max(c.startTime - F(), 0), u.entries = [c], s());
338-
}, h = x("largest-contentful-paint", m);
338+
}, h = x("largest-contentful-paint", p);
339339
if (h) {
340340
s = b(n, u, vt, o.reportAllChanges);
341341
const T = et(() => {
342-
m(h.takeRecords()), h.disconnect(), s(!0);
342+
p(h.takeRecords()), h.disconnect(), s(!0);
343343
});
344344
for (const c of ["keydown", "click", "visibilitychange"]) addEventListener(c, () => X(T), { capture: !0, once: !0 });
345345
P((c) => {
@@ -355,8 +355,8 @@ const vt = [2500, 4e3], Bt = (e, t = {}) => {
355355
if (a.entries.length) {
356356
const u = I();
357357
if (u) {
358-
const g = u.activationStart || 0, m = a.entries.at(-1), h = m.url && performance.getEntriesByType("resource").filter((d) => d.name === m.url)[0], T = Math.max(0, u.responseStart - g), c = Math.max(T, h ? (h.requestStart || h.startTime) - g : 0), l = Math.min(a.value, Math.max(c, h ? h.responseEnd - g : 0));
359-
s = { target: i.get(m), timeToFirstByte: T, resourceLoadDelay: c - T, resourceLoadDuration: l - c, elementRenderDelay: a.value - l, navigationEntry: u, lcpEntry: m }, m.url && (s.url = m.url), h && (s.lcpResourceEntry = h);
358+
const g = u.activationStart || 0, p = a.entries.at(-1), h = p.url && performance.getEntriesByType("resource").filter((d) => d.name === p.url)[0], T = Math.max(0, u.responseStart - g), c = Math.max(T, h ? (h.requestStart || h.startTime) - g : 0), l = Math.min(a.value, Math.max(c, h ? h.responseEnd - g : 0));
359+
s = { target: i.get(p), timeToFirstByte: T, resourceLoadDelay: c - T, resourceLoadDuration: l - c, elementRenderDelay: a.value - l, navigationEntry: u, lcpEntry: p }, p.url && (s.url = p.url), h && (s.lcpResourceEntry = h);
360360
}
361361
}
362362
return Object.assign(a, { attribution: s });
@@ -378,8 +378,8 @@ const vt = [2500, 4e3], Bt = (e, t = {}) => {
378378
const i = ((n) => {
379379
let o = { waitingDuration: 0, cacheDuration: 0, dnsDuration: 0, connectionDuration: 0, requestDuration: 0 };
380380
if (n.entries.length) {
381-
const a = n.entries[0], s = a.activationStart || 0, u = Math.max((a.workerStart || a.fetchStart) - s, 0), g = Math.max(a.domainLookupStart - s, 0), m = Math.max(a.connectStart - s, 0), h = Math.max(a.connectEnd - s, 0);
382-
o = { waitingDuration: u, cacheDuration: g - u, dnsDuration: m - g, connectionDuration: h - m, requestDuration: n.value - h, navigationEntry: a };
381+
const a = n.entries[0], s = a.activationStart || 0, u = Math.max((a.workerStart || a.fetchStart) - s, 0), g = Math.max(a.domainLookupStart - s, 0), p = Math.max(a.connectStart - s, 0), h = Math.max(a.connectEnd - s, 0);
382+
o = { waitingDuration: u, cacheDuration: g - u, dnsDuration: p - g, connectionDuration: h - p, requestDuration: n.value - h, navigationEntry: a };
383383
}
384384
return Object.assign(n, { attribution: o });
385385
})(r);
@@ -391,9 +391,10 @@ function Nt(e) {
391391
const r = {
392392
value: t.value,
393393
cache: t.attribution.cacheDuration,
394-
connection: t.attribution.connectionDuration ?? 0,
395-
request: t.attribution.requestDuration ?? 0,
396-
waiting: t.attribution.waitingDuration ?? 0,
394+
dns: t.attribution.dnsDuration,
395+
connection: t.attribution.connectionDuration,
396+
request: t.attribution.requestDuration,
397+
waiting: t.attribution.waitingDuration,
397398
isCached: !1,
398399
navigationType: t.navigationType,
399400
serverTiming: {}

0 commit comments

Comments
 (0)